/[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.10 by sysadm, Fri May 23 00:12:59 2025 UTC Revision 1.23 by sysadm, Mon May 26 03:20:39 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
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 86  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 102  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 111  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 155  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                  free(p_article_block_pool);          if (shmdt(p_article_block_pool) == -1)
227                  p_article_block_pool = NULL;          {
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  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 294  ARTICLE *article_block_find_by_index(int Line 355  ARTICLE *article_block_find_by_index(int
355    
356          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)
357          {          {
358                  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);
359                  return NULL;                  return NULL;
360          }          }
361    
# Line 302  ARTICLE *article_block_find_by_index(int Line 363  ARTICLE *article_block_find_by_index(int
363    
364          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)
365          {          {
366                  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);
367                  return NULL;                  return NULL;
368          }          }
369    
370          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
371  }  }
372    
373  SECTION_LIST *section_list_create(const char *sname, const char *stitle, const char *master_name)  extern int section_list_init(const char *filename)
374  {  {
375          SECTION_LIST *p_section;          int semid;
376            int shmid;
377            int proj_id;
378            key_t key;
379            size_t size;
380            void *p_shm;
381            union semun arg;
382            int i;
383    
384          if (p_section_list_pool == NULL)          if (p_section_list_pool != NULL)
385          {          {
386                  p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));                  section_list_cleanup();
387                  if (p_section_list_pool == NULL)          }
388                  {  
389                          log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);          proj_id = (int)(time(NULL) % getpid());
390                          return NULL;          key = ftok(filename, proj_id);
391                  }          if (key == -1)
392            {
393                    log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
394                    return -3;
395            }
396    
397                  section_list_count = 0;          // Allocate shared memory
398            size = sizeof(SECTION_LIST_POOL);
399            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
400            if (shmid == -1)
401            {
402                    log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
403                    return -3;
404            }
405            p_shm = shmat(shmid, NULL, 0);
406            if (p_shm == (void *)-1)
407            {
408                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
409                    return -3;
410            }
411    
412            p_section_list_pool = p_shm;
413            p_section_list_pool->shmid = shmid;
414            p_section_list_pool->section_count = 0;
415    
416            // Allocate semaphore as section locks
417            size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
418            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
419            if (semid == -1)
420            {
421                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
422                    return -3;
423          }          }
424    
425          if (p_trie_dict_section_list == NULL)          // Initialize sem value to 0
426            arg.val = 0;
427            for (i = 0; i < size; i++)
428          {          {
429                  p_trie_dict_section_list = trie_dict_create();                  if (semctl(semid, i, SETVAL, arg) == -1)
                 if (p_trie_dict_section_list == NULL)  
430                  {                  {
431                          log_error("trie_dict_create() OOM\n", BBS_max_section);                          log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
432                          return NULL;                          return -3;
433                  }                  }
434          }          }
435    
436          if (section_list_count >= BBS_max_section)          p_section_list_pool->semid = semid;
437    
438            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
439            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
440            {
441                    log_error("trie_dict_create() OOM\n", BBS_max_section);
442                    return -2;
443            }
444    
445            p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
446            if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
447            {
448                    log_error("trie_dict_create() OOM\n", BBS_max_section);
449                    return -2;
450            }
451    
452            return 0;
453    }
454    
455    inline static void sid_to_str(int32_t sid, char *p_sid_str)
456    {
457            uint32_t u_sid;
458            int i;
459    
460            u_sid = (uint32_t)sid;
461            for (i = 0; i < SID_STR_LEN - 1; i++)
462            {
463                    p_sid_str[i] = (char)(u_sid % 255 + 1);
464                    u_sid /= 255;
465            }
466            p_sid_str[i] = '\0';
467    }
468    
469    SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)
470    {
471            SECTION_LIST *p_section;
472            char sid_str[SID_STR_LEN];
473    
474            if (p_section_list_pool == NULL)
475          {          {
476                  log_error("section_list_count exceed limit %d\n", BBS_max_section);                  log_error("session_list_pool not initialized\n");
477                  return NULL;                  return NULL;
478          }          }
479    
480          p_section = p_section_list_pool + section_list_count;          if (p_section_list_pool->section_count >= BBS_max_section)
481            {
482                    log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section);
483                    return NULL;
484            }
485    
486            sid_to_str(sid, sid_str);
487    
488            p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
489    
490            p_section->sid = sid;
491    
492          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));
493          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname - 1)] = '\0';
# Line 352  SECTION_LIST *section_list_create(const Line 498  SECTION_LIST *section_list_create(const
498          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));
499          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';
500    
501          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)
502          {          {
503                  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);
504                    return NULL;
505            }
506    
507            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
508            {
509                    log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count);
510                  return NULL;                  return NULL;
511          }          }
512    
513          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
514    
515          section_list_count++;          p_section_list_pool->section_count++;
516    
517          return p_section;          return p_section;
518  }  }
# Line 380  void section_list_reset_articles(SECTION Line 532  void section_list_reset_articles(SECTION
532    
533  void section_list_cleanup(void)  void section_list_cleanup(void)
534  {  {
535          if (p_trie_dict_section_list != NULL)          int shmid;
536    
537            if (p_section_list_pool == NULL)
538            {
539                    return;
540            }
541    
542            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
543          {          {
544                  trie_dict_destroy(p_trie_dict_section_list);                  trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
545                  p_trie_dict_section_list = NULL;                  p_section_list_pool->p_trie_dict_section_by_name = NULL;
546            }
547    
548            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
549            {
550                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
551                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
552          }          }
553    
554          if (p_section_list_pool != NULL)          if (p_section_list_pool != NULL)
555          {          {
556                  free(p_section_list_pool);                  if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
557                    {
558                            log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
559                    }
560    
561                    shmid = p_section_list_pool->shmid;
562    
563                    if (shmdt(p_section_list_pool) == -1)
564                    {
565                            log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
566                    }
567    
568                    if (shmctl(shmid, IPC_RMID, NULL) == -1)
569                    {
570                            log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
571                    }
572    
573                  p_section_list_pool = NULL;                  p_section_list_pool = NULL;
574          }          }
   
         section_list_count = 0;  
575  }  }
576    
577  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
578  {  {
579          int64_t index;          int64_t index;
580    
581          if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL)          if (p_section_list_pool == NULL)
582            {
583                    log_error("section_list not initialized\n");
584                    return NULL;
585            }
586    
587            if (trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index) != 1)
588            {
589                    log_error("trie_dict_get(section, %s) error\n", sname);
590                    return NULL;
591            }
592    
593            return (p_section_list_pool->sections + index);
594    }
595    
596    SECTION_LIST *section_list_find_by_sid(int32_t sid)
597    {
598            int64_t index;
599            char sid_str[SID_STR_LEN];
600    
601            if (p_section_list_pool == NULL)
602          {          {
603                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
604                  return NULL;                  return NULL;
605          }          }
606    
607          if (trie_dict_get(p_trie_dict_section_list, sname, &index) != 1)          sid_to_str(sid, sid_str);
608    
609            if (trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index) != 1)
610          {          {
611                  log_error("trie_dict_get(section_data, %s) error\n", sname);                  log_error("trie_dict_get(section, %d) error\n", sid);
612                  return NULL;                  return NULL;
613          }          }
614    
615          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
616  }  }
617    
618  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 434  int section_list_append_article(SECTION_ Line 635  int section_list_append_article(SECTION_
635                  return -1;                  return -1;
636          }          }
637    
638            if (p_section->sid != p_article_src->sid)
639            {
640                    log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid);
641                    return -2;
642            }
643    
644          if (p_section->article_count >= BBS_article_limit_per_section)          if (p_section->article_count >= BBS_article_limit_per_section)
645          {          {
646                  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 466  int section_list_append_article(SECTION_ Line 673  int section_list_append_article(SECTION_
673          // AID of articles should be strictly ascending          // AID of articles should be strictly ascending
674          if (p_article_src->aid <= last_aid)          if (p_article_src->aid <= last_aid)
675          {          {
676                  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);
677                  return -3;                  return -3;
678          }          }
679    
# Line 530  int section_list_append_article(SECTION_ Line 737  int section_list_append_article(SECTION_
737          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
738    
739          // Update page          // Update page
740          if (p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0)          if ((p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) ||
741                    p_section->article_count == 1)
742          {          {
743                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
744                  p_section->page_count++;                  p_section->page_count++;
# Line 563  int section_list_set_article_visible(SEC Line 771  int section_list_set_article_visible(SEC
771                  return -1; // Not found                  return -1; // Not found
772          }          }
773    
774            if (p_section->sid != p_article->sid)
775            {
776                    log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid);
777                    return -2;
778            }
779    
780          if (p_article->visible == visible)          if (p_article->visible == visible)
781          {          {
782                  return 0; // Already set                  return 0; // Already set
# Line 610  int section_list_set_article_visible(SEC Line 824  int section_list_set_article_visible(SEC
824          return affected_count;          return affected_count;
825  }  }
826    
827  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset)  ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)
828  {  {
829          ARTICLE *p_article;          ARTICLE *p_article;
830          int left;          int left;
# Line 619  ARTICLE *section_list_find_article_with_ Line 833  ARTICLE *section_list_find_article_with_
833    
834          *p_page = -1;          *p_page = -1;
835          *p_offset = -1;          *p_offset = -1;
836            *pp_next = NULL;
837    
838          if (p_section == NULL)          if (p_section == NULL)
839          {          {
# Line 663  ARTICLE *section_list_find_article_with_ Line 878  ARTICLE *section_list_find_article_with_
878          p_article = p_section->p_page_first_article[*p_page];          p_article = p_section->p_page_first_article[*p_page];
879    
880          // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid          // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid
881          right = (*p_page == MAX(1, p_section->page_count) - 1 ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);          right = (*p_page == MAX(0, p_section->page_count - 1) ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);
882    
883          // left will be the offset of article found or offset to insert          // left will be the offset of article found or offset to insert
884          left = 0;          left = 0;
885    
886          while (aid > p_article->aid)          while (aid > p_article->aid)
887          {          {
                 // aid > p_article->aid  
888                  p_article = p_article->p_next;                  p_article = p_article->p_next;
889                  left++;                  left++;
890    
891                    if (aid == p_article->aid)
892                    {
893                            *pp_next = p_article->p_next;
894                            break;
895                    }
896    
897                  // over last article in the page                  // over last article in the page
898                  if (p_article == p_section->p_article_head || p_article->aid >= right)                  if (p_article == p_section->p_article_head || p_article->aid >= right)
899                  {                  {
900                          break;                          *pp_next = (p_article == p_section->p_article_head ? p_section->p_article_head : p_section->p_page_first_article[*p_page + 1]);
901                            *p_offset = left;
902                            return NULL; // not found
903                  }                  }
904          }          }
905    
906          if (aid != p_article->aid) // not exist          if (aid < p_article->aid)
907          {          {
908                  p_article = NULL;                  *pp_next = p_article;
909                    p_article = NULL; // not found
910            }
911            else // aid == p_article->aid
912            {
913                    *pp_next = p_article->p_next;
914          }          }
915    
916          *p_offset = left;          *p_offset = left;
# Line 694  ARTICLE *section_list_find_article_with_ Line 921  ARTICLE *section_list_find_article_with_
921  int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)  int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)
922  {  {
923          ARTICLE *p_article;          ARTICLE *p_article;
924            ARTICLE *p_next;
925          int32_t page;          int32_t page;
926          int32_t offset;          int32_t offset;
927          int visible_article_count;          int visible_article_count;
# Line 705  int section_list_calculate_page(SECTION_ Line 933  int section_list_calculate_page(SECTION_
933                  return -1;                  return -1;
934          }          }
935    
936          p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset);          if (p_section->article_count == 0) // empty
937          if (p_article == NULL)          {
938                    p_section->page_count = 0;
939                    p_section->last_page_visible_article_count = 0;
940    
941                    return 0;
942            }
943    
944            if (start_aid > 0)
945          {          {
946                  if (page < 0)                  p_article = article_block_find_by_aid(start_aid);
947                    if (p_article == NULL)
948                  {                  {
949                          return 0;                          return -1; // Not found
950                  }                  }
951    
952                  log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n", start_aid, p_section->sid);                  if (p_section->sid != p_article->sid)
953          }                  {
954                            log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid);
955                            return -2;
956                    }
957    
958                    p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);
959                    if (p_article == NULL)
960                    {
961                            if (page < 0)
962                            {
963                                    return -1;
964                            }
965                            log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n",
966                                              start_aid, p_section->sid);
967                            return -2;
968                    }
969    
970          if (offset > 0)                  if (offset > 0)
971                    {
972                            p_article = p_section->p_page_first_article[page];
973                    }
974            }
975            else
976          {          {
977                  p_article = p_section->p_page_first_article[page];                  p_article = p_section->p_article_head;
978                    page = 0;
979                    offset = 0;
980          }          }
981    
982          visible_article_count = 0;          visible_article_count = 0;
# Line 764  int section_list_calculate_page(SECTION_ Line 1022  int section_list_calculate_page(SECTION_
1022    
1023          return 0;          return 0;
1024  }  }
1025    
1026    int article_count_of_topic(int32_t aid)
1027    {
1028            ARTICLE *p_article;
1029            int article_count;
1030    
1031            p_article = article_block_find_by_aid(aid);
1032            if (p_article == NULL)
1033            {
1034                    return 0; // Not found
1035            }
1036    
1037            article_count = 0;
1038    
1039            do
1040            {
1041                    if (p_article->tid != 0 && p_article->tid != aid)
1042                    {
1043                            log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid);
1044                            break;
1045                    }
1046    
1047                    article_count++;
1048                    p_article = p_article->p_topic_next;
1049            } while (p_article->aid != aid);
1050    
1051            return article_count;
1052    }
1053    
1054    int section_list_move_topic(SECTION_LIST *p_section_src, SECTION_LIST *p_section_dest, int32_t aid)
1055    {
1056            ARTICLE *p_article;
1057            ARTICLE *p_next;
1058            int32_t page;
1059            int32_t offset;
1060            int32_t move_article_count;
1061            int32_t dest_article_count_old;
1062            int32_t last_unaffected_aid_src;
1063            int32_t first_inserted_aid_dest;
1064            int move_counter;
1065    
1066            if (p_section_dest == NULL)
1067            {
1068                    log_error("section_list_move_topic() NULL pointer error\n");
1069                    return -1;
1070            }
1071    
1072            if ((p_article = article_block_find_by_aid(aid)) == NULL)
1073            {
1074                    log_error("section_list_move_topic() error: article %d not found in block\n", aid);
1075                    return -2;
1076            }
1077    
1078            if (p_section_src->sid != p_article->sid)
1079            {
1080                    log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1081                                      p_section_src->sid, p_article->aid, p_article->sid);
1082                    return -2;
1083            }
1084    
1085            if (p_article->tid != 0)
1086            {
1087                    log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d\n", aid, p_article->tid);
1088                    return -2;
1089            }
1090    
1091            last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);
1092    
1093            move_article_count = article_count_of_topic(aid);
1094            if (move_article_count <= 0)
1095            {
1096                    log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1097                    return -2;
1098            }
1099    
1100            if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section)
1101            {
1102                    log_error("section_list_move_topic() error: article_count %d reach limit in section %d\n",
1103                                      p_section_dest->article_count + move_article_count, p_section_dest->sid);
1104                    return -3;
1105            }
1106    
1107            dest_article_count_old = p_section_dest->article_count;
1108            move_counter = 0;
1109            first_inserted_aid_dest = p_article->aid;
1110    
1111            do
1112            {
1113                    if (p_section_src->sid != p_article->sid)
1114                    {
1115                            log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",
1116                                              p_section_src->sid, p_article->aid, p_article->sid);
1117                            return -2;
1118                    }
1119    
1120                    // Remove from bi-directional article list of src section
1121                    if (p_section_src->p_article_head == p_article)
1122                    {
1123                            p_section_src->p_article_head = p_article->p_next;
1124                    }
1125                    if (p_section_src->p_article_tail == p_article)
1126                    {
1127                            p_section_src->p_article_tail = p_article->p_prior;
1128                    }
1129                    if (p_section_src->p_article_head == p_article) // || p_section_src->p_article_tail == p_article
1130                    {
1131                            p_section_src->p_article_head = NULL;
1132                            p_section_src->p_article_tail = NULL;
1133                    }
1134    
1135                    p_article->p_prior->p_next = p_article->p_next;
1136                    p_article->p_next->p_prior = p_article->p_prior;
1137    
1138                    // Update sid of article
1139                    p_article->sid = p_section_dest->sid;
1140    
1141                    if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1142                    {
1143                            log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1144                            return -4;
1145                    }
1146    
1147                    // Insert into bi-directional article list of dest section
1148                    if (p_next == NULL) // empty section
1149                    {
1150                            p_section_dest->p_article_head = p_article;
1151                            p_section_dest->p_article_tail = p_article;
1152                            p_article->p_prior = p_article;
1153                            p_article->p_next = p_article;
1154                    }
1155                    else
1156                    {
1157                            if (p_section_dest->p_article_head == p_next)
1158                            {
1159                                    if (p_article->aid < p_next->aid)
1160                                    {
1161                                            p_section_dest->p_article_head = p_article;
1162                                    }
1163                                    else // p_article->aid > p_next->aid
1164                                    {
1165                                            p_section_dest->p_article_tail = p_article;
1166                                    }
1167                            }
1168    
1169                            p_article->p_prior = p_next->p_prior;
1170                            p_article->p_next = p_next;
1171                            p_next->p_prior->p_next = p_article;
1172                            p_next->p_prior = p_article;
1173                    }
1174    
1175                    // Update article / topic counter of src / desc section
1176                    p_section_src->article_count--;
1177                    p_section_dest->article_count++;
1178                    if (p_article->tid == 0)
1179                    {
1180                            p_section_src->topic_count--;
1181                            p_section_dest->topic_count++;
1182                    }
1183    
1184                    // Update visible article / topic counter of src / desc section
1185                    if (p_article->visible)
1186                    {
1187                            p_section_src->visible_article_count--;
1188                            p_section_dest->visible_article_count++;
1189                            if (p_article->tid == 0)
1190                            {
1191                                    p_section_src->visible_topic_count--;
1192                                    p_section_dest->visible_topic_count++;
1193                            }
1194                    }
1195    
1196                    // Update page for empty dest section
1197                    if (p_section_dest->article_count == 1)
1198                    {
1199                            p_section_dest->p_page_first_article[0] = p_article;
1200                            p_section_dest->page_count = 1;
1201                            p_section_dest->last_page_visible_article_count = (p_article->visible ? 1 : 0);
1202                    }
1203    
1204                    p_article = p_article->p_topic_next;
1205    
1206                    move_counter++;
1207                    if (move_counter % CALCULATE_PAGE_THRESHOLD == 0)
1208                    {
1209                            // Re-calculate pages of desc section
1210                            if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1211                            {
1212                                    log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1213                                                      p_section_dest->sid, first_inserted_aid_dest);
1214                            }
1215    
1216                            first_inserted_aid_dest = p_article->aid;
1217                    }
1218            } while (p_article->aid != aid);
1219    
1220            if (p_section_dest->article_count - dest_article_count_old != move_article_count)
1221            {
1222                    log_error("section_list_move_topic() error: count of moved articles %d != %d\n",
1223                                      p_section_dest->article_count - dest_article_count_old, move_article_count);
1224            }
1225    
1226            // Re-calculate pages of src section
1227            if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
1228            {
1229                    log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n",
1230                                      p_section_src->sid, last_unaffected_aid_src, aid);
1231            }
1232    
1233            if (move_counter % CALCULATE_PAGE_THRESHOLD != 0)
1234            {
1235                    // Re-calculate pages of desc section
1236                    if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0)
1237                    {
1238                            log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n",
1239                                              p_section_dest->sid, first_inserted_aid_dest);
1240                    }
1241            }
1242    
1243            return move_article_count;
1244    }
1245    
1246    int get_section_index(SECTION_LIST *p_section)
1247    {
1248            int index;
1249    
1250            if (p_section_list_pool == NULL)
1251            {
1252                    log_error("get_section_index() error: uninitialized\n");
1253                    return -1;
1254            }
1255    
1256            if (p_section == NULL)
1257            {
1258                    index = BBS_max_section;
1259            }
1260            else
1261            {
1262                    index = (int)(p_section - p_section_list_pool->sections);
1263                    if (index < 0 || index >= BBS_max_section)
1264                    {
1265                            log_error("get_section_index(%d) error: index out of range\n", index);
1266                            return -2;
1267                    }
1268            }
1269    
1270            return index;
1271    }
1272    
1273    int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1274    {
1275            int index;
1276            struct sembuf sops[4];
1277            struct timespec timeout;
1278            int ret;
1279    
1280            index = get_section_index(p_section);
1281            if (index < 0)
1282            {
1283                    return -2;
1284            }
1285    
1286            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1287            sops[0].sem_op = 0;                                                                // wait until unlocked
1288            sops[0].sem_flg = 0;
1289    
1290            sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1291            sops[1].sem_op = 1;                                                        // lock
1292            sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1293    
1294            // Read lock on any specific section will also acquire single read lock on "all section"
1295            // so that write lock on all section only need to acquire single write on on "all section"
1296            // rather than to acquire multiple write locks on all the available sections.
1297            if (index != BBS_max_section)
1298            {
1299                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1300                    sops[2].sem_op = 0;                                                // wait until unlocked
1301                    sops[2].sem_flg = 0;
1302    
1303                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1304                    sops[3].sem_op = 1;                                        // lock
1305                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1306            }
1307    
1308            timeout.tv_sec = wait_sec;
1309            timeout.tv_nsec = 0;
1310    
1311            ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1312            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1313            {
1314                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1315            }
1316    
1317            return ret;
1318    }
1319    
1320    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1321    {
1322            int index;
1323            struct sembuf sops[3];
1324            struct timespec timeout;
1325            int ret;
1326    
1327            index = get_section_index(p_section);
1328            if (index < 0)
1329            {
1330                    return -2;
1331            }
1332    
1333            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1334            sops[0].sem_op = 0;                                                                // wait until unlocked
1335            sops[0].sem_flg = 0;
1336    
1337            sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1338            sops[1].sem_op = 1;                                                                // lock
1339            sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1340    
1341            sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1342            sops[2].sem_op = 0;                                                        // wait until unlocked
1343            sops[2].sem_flg = 0;
1344    
1345            timeout.tv_sec = wait_sec;
1346            timeout.tv_nsec = 0;
1347    
1348            ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1349            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1350            {
1351                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1352            }
1353    
1354            return ret;
1355    }
1356    
1357    int section_list_rd_unlock(SECTION_LIST *p_section)
1358    {
1359            int index;
1360            struct sembuf sops[2];
1361            int ret;
1362    
1363            index = get_section_index(p_section);
1364            if (index < 0)
1365            {
1366                    return -2;
1367            }
1368    
1369            sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1370            sops[0].sem_op = -1;                                               // unlock
1371            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1372    
1373            if (index != BBS_max_section)
1374            {
1375                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1376                    sops[1].sem_op = -1;                                     // unlock
1377                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1378            }
1379    
1380            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1381            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1382            {
1383                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1384            }
1385    
1386            return ret;
1387    }
1388    
1389    int section_list_rw_unlock(SECTION_LIST *p_section)
1390    {
1391            int index;
1392            struct sembuf sops[1];
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 = -1;                                                       // unlock
1403            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1404    
1405            ret = semop(p_section_list_pool->semid, sops, 1);
1406            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1407            {
1408                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1409            }
1410    
1411            return ret;
1412    }
1413    
1414    int section_list_rd_lock(SECTION_LIST *p_section)
1415    {
1416            int timer = 0;
1417            int sid = (p_section == NULL ? 0 : p_section->sid);
1418            int ret = -1;
1419    
1420            while (!SYS_server_exit)
1421            {
1422                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1423                    if (ret == 0) // success
1424                    {
1425                            break;
1426                    }
1427                    else if (errno == EAGAIN || errno == EINTR) // retry
1428                    {
1429                            timer++;
1430                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1431                            {
1432                                    log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer);
1433                            }
1434                    }
1435                    else // failed
1436                    {
1437                            log_error("section_list_rd_lock() failed on section %d\n", sid);
1438                            break;
1439                    }
1440            }
1441    
1442            return ret;
1443    }
1444    
1445    int section_list_rw_lock(SECTION_LIST *p_section)
1446    {
1447            int timer = 0;
1448            int sid = (p_section == NULL ? 0 : p_section->sid);
1449            int ret = -1;
1450    
1451            while (!SYS_server_exit)
1452            {
1453                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1454                    if (ret == 0) // success
1455                    {
1456                            break;
1457                    }
1458                    else if (errno == EAGAIN || errno == EINTR) // retry
1459                    {
1460                            timer++;
1461                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1462                            {
1463                                    log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer);
1464                            }
1465                    }
1466                    else // failed
1467                    {
1468                            log_error("acquire_section_rw_lock() failed on section %d\n", sid);
1469                            break;
1470                    }
1471            }
1472    
1473            return ret;
1474    }


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

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