/[LeafOK_CVS]/lbbs/src/section_list.c
ViewVC logotype

Diff of /lbbs/src/section_list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.16 by sysadm, Sat May 24 07:32:46 2025 UTC Revision 1.48 by sysadm, Wed Oct 29 01:42:06 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #define _GNU_SOURCE  
   
 #include "section_list.h"  
17  #include "log.h"  #include "log.h"
18    #include "section_list.h"
19  #include "trie_dict.h"  #include "trie_dict.h"
20    #include "user_list.h"
21    #include <errno.h>
22    #include <signal.h>
23  #include <stdio.h>  #include <stdio.h>
24    #include <stdlib.h>
25  #include <string.h>  #include <string.h>
 #include <signal.h>  
26  #include <unistd.h>  #include <unistd.h>
27  #include <stdlib.h>  #include <sys/ipc.h>
 #include <errno.h>  
28  #include <sys/param.h>  #include <sys/param.h>
29  #include <sys/sem.h>  #include <sys/sem.h>
30  #include <sys/shm.h>  #include <sys/shm.h>
 #include <sys/ipc.h>  
31    
32  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #ifdef _SEM_SEMUN_UNDEFINED
33  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  union semun
34    {
35            int val;                           /* Value for SETVAL */
36            struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
37            unsigned short *array; /* Array for GETALL, SETALL */
38            struct seminfo *__buf; /* Buffer for IPC_INFO
39                                                              (Linux-specific) */
40    };
41    #endif // #ifdef _SEM_SEMUN_UNDEFINED
42    
43    #define SECTION_TRY_LOCK_WAIT_TIME 1 // second
44    #define SECTION_TRY_LOCK_TIMES 10
45    
46    #define ARTICLE_BLOCK_PER_SHM 1000               // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
47    #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
48  #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)
49    
50  #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
51    
52  #define SID_STR_LEN 5 // 32-bit + NULL  #define SID_STR_LEN 5 // 32-bit + NULL
53    
54  struct article_block_t  struct article_block_t
55  {  {
56          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[ARTICLE_PER_BLOCK];
57          int32_t article_count;          int article_count;
58          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
59  };  };
60  typedef struct article_block_t ARTICLE_BLOCK;  typedef struct article_block_t ARTICLE_BLOCK;
61    
 struct article_block_shm_t  
 {  
         int shmid;  
         void *p_shm;  
 };  
 typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  
   
62  struct article_block_pool_t  struct article_block_pool_t
63  {  {
64          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          int shmid;
65            struct
66            {
67                    int shmid;
68                    void *p_shm;
69            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
70          int shm_count;          int shm_count;
71          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
72          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
73          int32_t block_count;          int block_count;
74  };  };
75  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
76    
77  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
78    
79  static int section_list_pool_shmid;  SECTION_LIST_POOL *p_section_list_pool = NULL;
 static int section_list_pool_semid;  
 static SECTION_LIST *p_section_list_pool = NULL;  
 static int section_list_count = 0;  
 static TRIE_NODE *p_trie_dict_section_by_name = NULL;  
 static TRIE_NODE *p_trie_dict_section_by_sid = NULL;  
80    
81  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
82  {  {
# Line 90  int article_block_init(const char *filen Line 96  int article_block_init(const char *filen
96                  return -1;                  return -1;
97          }          }
98    
99          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
100          {          {
101                  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);
102                  return -2;                  return -2;
103          }          }
104    
105          p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL));          // Allocate shared memory
106          if (p_article_block_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
107            key = ftok(filename, proj_id);
108            if (key == -1)
109          {          {
110                  log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n");                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
111                  return -2;                  return -3;
112          }          }
113    
114          // Allocate shared memory          size = sizeof(ARTICLE_BLOCK_POOL);
115            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
116            if (shmid == -1)
117            {
118                    log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
119                    return -3;
120            }
121            p_shm = shmat(shmid, NULL, 0);
122            if (p_shm == (void *)-1)
123            {
124                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
125                    return -3;
126            }
127    
128            p_article_block_pool = p_shm;
129            p_article_block_pool->shmid = shmid;
130    
131          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
132          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
133    
# Line 112  int article_block_init(const char *filen Line 136  int article_block_init(const char *filen
136                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
137                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
138    
139                  proj_id = getpid() + p_article_block_pool->shm_count;                  proj_id = p_article_block_pool->shm_count;
140                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
141                  if (key == -1)                  if (key == -1)
142                  {                  {
# Line 121  int article_block_init(const char *filen Line 145  int article_block_init(const char *filen
145                          return -3;                          return -3;
146                  }                  }
147    
148                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
149                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
150                  if (shmid == -1)                  if (shmid == -1)
151                  {                  {
# Line 165  int article_block_init(const char *filen Line 189  int article_block_init(const char *filen
189    
190  void article_block_cleanup(void)  void article_block_cleanup(void)
191  {  {
192          if (p_article_block_pool != NULL)          int shmid;
193    
194            if (p_article_block_pool == NULL)
195          {          {
196                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  return;
197            }
198    
199            for (int i = 0; i < p_article_block_pool->shm_count; i++)
200            {
201                    if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
202                  {                  {
203                          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);
204                          {                  }
                                 log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);  
                         }  
205    
206                          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)
207                          {                  {
208                                  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);
209                          }                  }
210            }
211    
212            shmid = p_article_block_pool->shmid;
213    
214            if (shmdt(p_article_block_pool) == -1)
215            {
216                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
217            }
218    
219            if (shmctl(shmid, IPC_RMID, NULL) == -1)
220            {
221                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
222            }
223    
224            p_article_block_pool = NULL;
225    }
226    
227    int set_article_block_shm_readonly(void)
228    {
229            int shmid;
230            void *p_shm;
231            int i;
232    
233            if (p_article_block_pool == NULL)
234            {
235                    log_error("article_block_pool not initialized\n");
236                    return -1;
237            }
238    
239            for (i = 0; i < p_article_block_pool->shm_count; i++)
240            {
241                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
242    
243                    // Remap shared memory in read-only mode
244                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
245                    if (p_shm == (void *)-1)
246                    {
247                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
248                            return -2;
249                  }                  }
250            }
251    
252            return 0;
253    }
254    
255                  free(p_article_block_pool);  int detach_article_block_shm(void)
256                  p_article_block_pool = NULL;  {
257            int shmid;
258    
259            if (p_article_block_pool == NULL)
260            {
261                    return -1;
262          }          }
263    
264            for (int i = 0; i < p_article_block_pool->shm_count; i++)
265            {
266                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
267                    {
268                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
269                            return -2;
270                    }
271            }
272    
273            shmid = p_article_block_pool->shmid;
274    
275            if (shmdt(p_article_block_pool) == -1)
276            {
277                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
278                    return -3;
279            }
280    
281            p_article_block_pool = NULL;
282    
283            return 0;
284  }  }
285    
286  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 245  ARTICLE *article_block_find_by_aid(int32 Line 343  ARTICLE *article_block_find_by_aid(int32
343          }          }
344    
345          left = 0;          left = 0;
346          right = p_article_block_pool->block_count;          right = p_article_block_pool->block_count - 1;
347    
348          // aid in the range [ head aid of blocks[left], tail aid of blocks[right - 1] ]          // aid in the range [ head aid of blocks[left], tail aid of blocks[right] ]
349          while (left < right - 1)          while (left < right)
350          {          {
351                  // get block offset no less than mid value of left and right block offsets                  // get block offset no less than mid value of left and right block offsets
352                  mid = (left + right) / 2 + (right - left) % 2;                  mid = (left + right) / 2 + (left + right) % 2;
   
                 if (mid >= p_article_block_pool->block_count)  
                 {  
                         log_error("block(mid = %d) is out of boundary\n", mid);  
                         return NULL;  
                 }  
353    
354                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
355                  {                  {
356                          right = mid;                          right = mid - 1;
357                  }                  }
358                  else                  else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid)
359                  {                  {
360                          left = mid;                          left = mid;
361                  }                  }
# Line 289  ARTICLE *article_block_find_by_aid(int32 Line 381  ARTICLE *article_block_find_by_aid(int32
381                  }                  }
382          }          }
383    
384          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
385            {
386                    return NULL;
387            }
388    
389            return (p_block->articles + left); // found
390  }  }
391    
392  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 319  ARTICLE *article_block_find_by_index(int Line 416  ARTICLE *article_block_find_by_index(int
416          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
417  }  }
418    
419  extern int section_list_pool_init(const char *filename)  extern int section_list_init(const char *filename)
420  {  {
421          int semid;          int semid;
422          int shmid;          int shmid;
# Line 327  extern int section_list_pool_init(const Line 424  extern int section_list_pool_init(const
424          key_t key;          key_t key;
425          size_t size;          size_t size;
426          void *p_shm;          void *p_shm;
427            union semun arg;
428            int i;
429    
430          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool != NULL)
         {  
                 section_list_pool_cleanup();  
         }  
   
         p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST));  
         if (p_section_list_pool == NULL)  
431          {          {
432                  log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);                  section_list_cleanup();
                 return -1;  
433          }          }
434    
435          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 348  extern int section_list_pool_init(const Line 440  extern int section_list_pool_init(const
440                  return -3;                  return -3;
441          }          }
442    
443          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections          // Allocate shared memory
444          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);          size = sizeof(SECTION_LIST_POOL);
         if (semid == -1)  
         {  
                 log_error("semget(section_list_pool, size = %d) error (%d)\n", size, errno);  
                 return -3;  
         }  
   
         section_list_pool_semid = semid;  
   
         size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section;  
445          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
446          if (shmid == -1)          if (shmid == -1)
447          {          {
448                  log_error("shmget(section_list_pool, size = %d) error (%d)\n", size, errno);                  log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
449                  return -3;                  return -3;
450          }          }
451          p_shm = shmat(shmid, NULL, 0);          p_shm = shmat(shmid, NULL, 0);
# Line 372  extern int section_list_pool_init(const Line 455  extern int section_list_pool_init(const
455                  return -3;                  return -3;
456          }          }
457    
         section_list_pool_shmid = shmid;  
458          p_section_list_pool = p_shm;          p_section_list_pool = p_shm;
459          section_list_count = 0;          p_section_list_pool->shmid = shmid;
460            p_section_list_pool->section_count = 0;
461    
462            // Allocate semaphore as section locks
463            size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
464            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
465            if (semid == -1)
466            {
467                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
468                    return -3;
469            }
470    
471            // Initialize sem value to 0
472            arg.val = 0;
473            for (i = 0; i < size; i++)
474            {
475                    if (semctl(semid, i, SETVAL, arg) == -1)
476                    {
477                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
478                            return -3;
479                    }
480            }
481    
482            p_section_list_pool->semid = semid;
483    
484          p_trie_dict_section_by_name = trie_dict_create();          p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
485          if (p_trie_dict_section_by_name == NULL)          if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
486          {          {
487                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
488                  return -2;                  return -2;
489          }          }
490    
491          p_trie_dict_section_by_sid = trie_dict_create();          p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
492          if (p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
493          {          {
494                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
495                  return -2;                  return -2;
# Line 393  extern int section_list_pool_init(const Line 498  extern int section_list_pool_init(const
498          return 0;          return 0;
499  }  }
500    
501    void section_list_cleanup(void)
502    {
503            int shmid;
504    
505            if (p_section_list_pool == NULL)
506            {
507                    return;
508            }
509    
510            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
511            {
512                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
513                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
514            }
515    
516            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
517            {
518                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
519                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
520            }
521    
522            shmid = p_section_list_pool->shmid;
523    
524            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
525            {
526                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
527            }
528    
529            if (shmdt(p_section_list_pool) == -1)
530            {
531                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
532            }
533    
534            if (shmctl(shmid, IPC_RMID, NULL) == -1)
535            {
536                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
537            }
538    
539            p_section_list_pool = NULL;
540    }
541    
542    int set_section_list_shm_readonly(void)
543    {
544            int shmid;
545            void *p_shm;
546    
547            if (p_section_list_pool == NULL)
548            {
549                    log_error("p_section_list_pool not initialized\n");
550                    return -1;
551            }
552    
553            shmid = p_section_list_pool->shmid;
554    
555            // Remap shared memory in read-only mode
556            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
557            if (p_shm == (void *)-1)
558            {
559                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
560                    return -3;
561            }
562    
563            p_section_list_pool = p_shm;
564    
565            return 0;
566    }
567    
568    int detach_section_list_shm(void)
569    {
570            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
571            {
572                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
573                    return -1;
574            }
575    
576            p_section_list_pool = NULL;
577    
578            return 0;
579    }
580    
581  inline static void sid_to_str(int32_t sid, char *p_sid_str)  inline static void sid_to_str(int32_t sid, char *p_sid_str)
582  {  {
583          uint32_t u_sid;          uint32_t u_sid;
# Line 407  inline static void sid_to_str(int32_t si Line 592  inline static void sid_to_str(int32_t si
592          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
593  }  }
594    
595  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_list)
596  {  {
597          SECTION_LIST *p_section;          SECTION_LIST *p_section;
598          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
599    
600          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL)
601          {          {
602                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized\n");
603                  return NULL;                  return NULL;
604          }          }
605    
606          if (section_list_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
607          {          {
608                  log_error("section_list_count exceed limit %d >= %d\n", section_list_count, BBS_max_section);                  log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section);
609                  return NULL;                  return NULL;
610          }          }
611    
612          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
613    
614          p_section = p_section_list_pool + section_list_count;          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
615    
616          p_section->sid = sid;          p_section->sid = sid;
617            p_section->ex_menu_tm = 0;
618    
619          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
620          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
621    
622          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
623          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
624    
625          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);
626          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
627    
628          if (trie_dict_set(p_trie_dict_section_by_name, 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)
629          {          {
630                  log_error("trie_dict_set(section, %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);
631                  return NULL;                  return NULL;
632          }          }
633    
634          if (trie_dict_set(p_trie_dict_section_by_sid, sid_str, section_list_count) != 1)          if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1)
635          {          {
636                  log_error("trie_dict_set(section, %d, %d) error\n", sid, section_list_count);                  log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count);
                 log_std("Debug %x %x %x %x\n", sid_str[0], sid_str[1], sid_str[2], sid_str[3]);  
637                  return NULL;                  return NULL;
638          }          }
639    
640          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
641    
642          section_list_count++;          p_section_list_pool->section_count++;
643    
644          return p_section;          return p_section;
645  }  }
646    
647    int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
648    {
649            int64_t index;
650    
651            if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
652            {
653                    log_error("NULL pointer error\n");
654                    return -1;
655            }
656    
657            index = get_section_index(p_section);
658    
659            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
660            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
661    
662            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
663            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
664    
665            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
666            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
667    
668            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
669            {
670                    log_error("trie_dict_set(section, %s, %d) error\n", sname, index);
671                    return -2;
672            }
673    
674            return 0;
675    }
676    
677  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
678  {  {
679          p_section->article_count = 0;          p_section->article_count = 0;
# Line 470  void section_list_reset_articles(SECTION Line 685  void section_list_reset_articles(SECTION
685    
686          p_section->page_count = 0;          p_section->page_count = 0;
687          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
 }  
   
 void section_list_pool_cleanup(void)  
 {  
         if (p_trie_dict_section_by_name != NULL)  
         {  
                 trie_dict_destroy(p_trie_dict_section_by_name);  
                 p_trie_dict_section_by_name = NULL;  
         }  
   
         if (p_trie_dict_section_by_sid != NULL)  
         {  
                 trie_dict_destroy(p_trie_dict_section_by_sid);  
                 p_trie_dict_section_by_sid = NULL;  
         }  
688    
689          if (p_section_list_pool != NULL)          p_section->ontop_article_count = 0;
         {  
                 if (shmdt(p_section_list_pool) == -1)  
                 {  
                         log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno);  
                 }  
                 p_section_list_pool = NULL;  
   
                 if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1)  
                 {  
                         log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno);  
                 }  
   
                 if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1)  
                 {  
                         log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno);  
                 }  
         }  
   
         section_list_count = 0;  
690  }  }
691    
692  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
693  {  {
694          int64_t index;          int64_t index;
695            int ret;
696    
697          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL)          if (p_section_list_pool == NULL)
698          {          {
699                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
700                  return NULL;                  return NULL;
701          }          }
702    
703          if (trie_dict_get(p_trie_dict_section_by_name, sname, &index) != 1)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index);
704            if (ret < 0)
705          {          {
706                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
707                  return NULL;                  return NULL;
708          }          }
709            else if (ret == 0)
710            {
711                    return NULL;
712            }
713    
714          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
715  }  }
716    
717  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
718  {  {
719          int64_t index;          int64_t index;
720            int ret;
721          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
722    
723          if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL)
724          {          {
725                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
726                  return NULL;                  return NULL;
# Line 540  SECTION_LIST *section_list_find_by_sid(i Line 728  SECTION_LIST *section_list_find_by_sid(i
728    
729          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
730    
731          if (trie_dict_get(p_trie_dict_section_by_sid, sid_str, &index) != 1)          ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index);
732            if (ret < 0)
733          {          {
734                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
735                  return NULL;                  return NULL;
736          }          }
737            else if (ret == 0)
738            {
739                    return NULL;
740            }
741    
742          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
743  }  }
744    
745  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 559  int section_list_append_article(SECTION_ Line 752  int section_list_append_article(SECTION_
752    
753          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
754          {          {
755                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
756                  return -1;                  return -1;
757          }          }
758    
# Line 684  int section_list_append_article(SECTION_ Line 877  int section_list_append_article(SECTION_
877                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
878          }          }
879    
880            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
881            {
882                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
883                                      p_section->sid, p_article->aid);
884                    return -5;
885            }
886    
887          return 0;          return 0;
888  }  }
889    
# Line 695  int section_list_set_article_visible(SEC Line 895  int section_list_set_article_visible(SEC
895    
896          if (p_section == NULL)          if (p_section == NULL)
897          {          {
898                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
899                  return -2;                  return -1;
900          }          }
901    
902          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 707  int section_list_set_article_visible(SEC Line 907  int section_list_set_article_visible(SEC
907    
908          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
909          {          {
910                  log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid);                  log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
911                  return -2;                  return -2;
912          }          }
913    
# Line 720  int section_list_set_article_visible(SEC Line 920  int section_list_set_article_visible(SEC
920          {          {
921                  p_section->visible_article_count--;                  p_section->visible_article_count--;
922    
923                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
924                    {
925                            log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);
926                    }
927    
928                  if (p_article->tid == 0)                  if (p_article->tid == 0)
929                  {                  {
930                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 738  int section_list_set_article_visible(SEC Line 943  int section_list_set_article_visible(SEC
943                                          p_reply->visible = 0;                                          p_reply->visible = 0;
944                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
945                                          affected_count++;                                          affected_count++;
946    
947                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
948                                            {
949                                                    log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);
950                                            }
951                                  }                                  }
952                          }                          }
953                  }                  }
# Line 750  int section_list_set_article_visible(SEC Line 960  int section_list_set_article_visible(SEC
960                  {                  {
961                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
962                  }                  }
963    
964                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
965                    {
966                            log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
967                    }
968          }          }
969    
970          p_article->visible = visible;          p_article->visible = visible;
# Line 758  int section_list_set_article_visible(SEC Line 973  int section_list_set_article_visible(SEC
973          return affected_count;          return affected_count;
974  }  }
975    
976    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
977    {
978            int i;
979    
980            if (p_section == NULL || p_article == NULL)
981            {
982                    log_error("NULL pointer error\n");
983                    return -1;
984            }
985    
986            if (p_section->sid != p_article->sid)
987            {
988                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
989                    return -2;
990            }
991    
992            if (p_article->ontop)
993            {
994                    for (i = 0; i < p_section->ontop_article_count; i++)
995                    {
996                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
997                            {
998                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
999                                    return 0;
1000                            }
1001                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
1002                            {
1003                                    break;
1004                            }
1005                    }
1006    
1007                    // Remove the oldest one if the array of ontop articles is full
1008                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
1009                    {
1010                            if (i == 0) // p_article is the oldest one
1011                            {
1012                                    return 0;
1013                            }
1014                            memmove((void *)(p_section->p_ontop_articles),
1015                                            (void *)(p_section->p_ontop_articles + 1),
1016                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1017                            p_section->ontop_article_count--;
1018                            i--;
1019                    }
1020                    else
1021                    {
1022                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1023                                            (void *)(p_section->p_ontop_articles + i),
1024                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1025                    }
1026    
1027                    p_section->p_ontop_articles[i] = p_article;
1028                    p_section->ontop_article_count++;
1029    
1030                    // TODO: debug
1031            }
1032            else // ontop == 0
1033            {
1034                    for (i = 0; i < p_section->ontop_article_count; i++)
1035                    {
1036                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1037                            {
1038                                    break;
1039                            }
1040                    }
1041                    if (i == p_section->ontop_article_count) // not found
1042                    {
1043                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1044                            return 0;
1045                    }
1046    
1047                    memmove((void *)(p_section->p_ontop_articles + i),
1048                                    (void *)(p_section->p_ontop_articles + i + 1),
1049                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1050                    p_section->ontop_article_count--;
1051            }
1052    
1053            return 0;
1054    }
1055    
1056    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1057    {
1058            int page_count;
1059    
1060            if (p_section == NULL)
1061            {
1062                    log_error("NULL pointer error\n");
1063                    return -1;
1064            }
1065    
1066            page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) +
1067                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1068                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1069    
1070            return page_count;
1071    }
1072    
1073    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1074    {
1075            if (p_section == NULL)
1076            {
1077                    log_error("NULL pointer error\n");
1078                    return -1;
1079            }
1080    
1081            if (page_id < p_section->page_count - 1)
1082            {
1083                    return BBS_article_limit_per_page;
1084            }
1085            else // if (page_id >= p_section->page_count - 1)
1086            {
1087                    return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1088                                               BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1089            }
1090    }
1091    
1092  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)  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)
1093  {  {
1094          ARTICLE *p_article;          ARTICLE *p_article;
# Line 771  ARTICLE *section_list_find_article_with_ Line 1102  ARTICLE *section_list_find_article_with_
1102    
1103          if (p_section == NULL)          if (p_section == NULL)
1104          {          {
1105                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1106                  return NULL;                  return NULL;
1107          }          }
1108    
# Line 783  ARTICLE *section_list_find_article_with_ Line 1114  ARTICLE *section_list_find_article_with_
1114          }          }
1115    
1116          left = 0;          left = 0;
1117          right = p_section->page_count;          right = p_section->page_count - 1;
1118    
1119          // aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ]          // aid in the range [ head aid of pages[left], tail aid of pages[right] ]
1120          while (left < right - 1)          while (left < right)
1121          {          {
1122                  // get page id no less than mid value of left page id and right page id                  // get page id no less than mid value of left page id and right page id
1123                  mid = (left + right) / 2 + (right - left) % 2;                  mid = (left + right) / 2 + (left + right) % 2;
   
                 if (mid >= p_section->page_count)  
                 {  
                         log_error("page id (mid = %d) is out of boundary\n", mid);  
                         return NULL;  
                 }  
1124    
1125                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1126                  {                  {
1127                          right = mid;                          right = mid - 1;
1128                  }                  }
1129                  else                  else // if (aid < p_section->p_page_first_article[mid]->aid)
1130                  {                  {
1131                          left = mid;                          left = mid;
1132                  }                  }
# Line 863  int section_list_calculate_page(SECTION_ Line 1188  int section_list_calculate_page(SECTION_
1188    
1189          if (p_section == NULL)          if (p_section == NULL)
1190          {          {
1191                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1192                  return -1;                  return -1;
1193          }          }
1194    
# Line 957  int section_list_calculate_page(SECTION_ Line 1282  int section_list_calculate_page(SECTION_
1282          return 0;          return 0;
1283  }  }
1284    
1285    int32_t article_block_last_aid(void)
1286    {
1287            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1288            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1289    
1290            return last_aid;
1291    }
1292    
1293    int article_block_article_count(void)
1294    {
1295            int ret;
1296    
1297            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1298            {
1299                    return -1;
1300            }
1301    
1302            ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1303                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1304    
1305            return ret;
1306    }
1307    
1308  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1309  {  {
1310          ARTICLE *p_article;          ARTICLE *p_article;
# Line 997  int section_list_move_topic(SECTION_LIST Line 1345  int section_list_move_topic(SECTION_LIST
1345          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1346          int move_counter;          int move_counter;
1347    
1348          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1349          {          {
1350                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("NULL pointer error\n");
1351                    return -1;
1352            }
1353    
1354            if (p_section_src->sid == p_section_dest->sid)
1355            {
1356                    log_error("section_list_move_topic() src and dest section are the same\n");
1357                  return -1;                  return -1;
1358          }          }
1359    
1360          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1361          {          {
1362                  log_error("section_list_move_topic() error: article %d not found in block\n", aid);                  log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid);
1363                  return -2;                  return -2;
1364          }          }
1365    
# Line 1027  int section_list_move_topic(SECTION_LIST Line 1381  int section_list_move_topic(SECTION_LIST
1381          move_article_count = article_count_of_topic(aid);          move_article_count = article_count_of_topic(aid);
1382          if (move_article_count <= 0)          if (move_article_count <= 0)
1383          {          {
1384                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1385                  return -2;                  return -2;
1386          }          }
1387    
# Line 1046  int section_list_move_topic(SECTION_LIST Line 1400  int section_list_move_topic(SECTION_LIST
1400          {          {
1401                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1402                  {                  {
1403                          log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n",                          log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n",
1404                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1405                          return -2;                          p_article = p_article->p_topic_next;
1406                            continue;
1407                  }                  }
1408    
1409                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1074  int section_list_move_topic(SECTION_LIST Line 1429  int section_list_move_topic(SECTION_LIST
1429    
1430                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)                  if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
1431                  {                  {
1432                          log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);                          log_error("section_list_move_topic() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
1433                          return -4;                          p_article = p_article->p_topic_next;
1434                            continue;
1435                  }                  }
1436    
1437                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1153  int section_list_move_topic(SECTION_LIST Line 1509  int section_list_move_topic(SECTION_LIST
1509    
1510          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)
1511          {          {
1512                  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",
1513                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1514          }          }
1515    
# Line 1193  int get_section_index(SECTION_LIST *p_se Line 1549  int get_section_index(SECTION_LIST *p_se
1549          }          }
1550          else          else
1551          {          {
1552                  index = (int)(p_section - p_section_list_pool);                  index = (int)(p_section - p_section_list_pool->sections);
1553                  if (index < 0 || index >= BBS_max_section)                  if (index < 0 || index >= BBS_max_section)
1554                  {                  {
1555                          log_error("get_section_index(%d) error: index out of range\n", index);                          log_error("get_section_index(%d) error: index out of range\n", index);
# Line 1204  int get_section_index(SECTION_LIST *p_se Line 1560  int get_section_index(SECTION_LIST *p_se
1560          return index;          return index;
1561  }  }
1562    
1563    int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list)
1564    {
1565            if (p_section == NULL)
1566            {
1567                    log_error("NULL pointer error\n");
1568                    return -1;
1569            }
1570    
1571            if (section_list_rd_lock(p_section) < 0)
1572            {
1573                    log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid);
1574                    return -2;
1575            }
1576    
1577            if (sname != NULL)
1578            {
1579                    memcpy(sname, p_section->sname, sizeof(p_section->sname));
1580            }
1581            if (stitle != NULL)
1582            {
1583                    memcpy(stitle, p_section->stitle, sizeof(p_section->stitle));
1584            }
1585            if (master_list != NULL)
1586            {
1587                    memcpy(master_list, p_section->master_list, sizeof(p_section->master_list));
1588            }
1589    
1590            // release lock of section
1591            if (section_list_rd_unlock(p_section) < 0)
1592            {
1593                    log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid);
1594                    return -2;
1595            }
1596    
1597            return 0;
1598    }
1599    
1600  int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)  int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1601  {  {
1602          int index;          int index;
1603          struct sembuf sops[2];          struct sembuf sops[4];
1604          struct timespec timeout;          struct timespec timeout;
1605          int ret;          int ret;
1606    
# Line 1217  int section_list_try_rd_lock(SECTION_LIS Line 1610  int section_list_try_rd_lock(SECTION_LIS
1610                  return -2;                  return -2;
1611          }          }
1612    
1613          sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index          sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1614          sops[0].sem_op = 0;                                                      // check if unlocked          sops[0].sem_op = 0;                                                                // wait until unlocked
1615          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
1616    
1617          sops[1].sem_num = (unsigned short)index; // r_sem of section index          sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1618          sops[1].sem_op = 1;                                              // lock          sops[1].sem_op = 1;                                                        // lock
1619          sops[1].sem_flg = SEM_UNDO;                              // undo on terminate          sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1620    
1621            // Read lock on any specific section will also acquire single read lock on "all section"
1622            // so that write lock on all section only need to acquire single write on on "all section"
1623            // rather than to acquire multiple write locks on all the available sections.
1624            if (index != BBS_max_section)
1625            {
1626                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1627                    sops[2].sem_op = 0;                                                // wait until unlocked
1628                    sops[2].sem_flg = 0;
1629    
1630                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1631                    sops[3].sem_op = 1;                                        // lock
1632                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1633            }
1634    
1635          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1636          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1637    
1638          ret = semtimedop(section_list_pool_semid, sops, 2, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1639            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1640            {
1641                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1642            }
1643    
1644          return ret;          return ret;
1645  }  }
# Line 1246  int section_list_try_rw_lock(SECTION_LIS Line 1657  int section_list_try_rw_lock(SECTION_LIS
1657                  return -2;                  return -2;
1658          }          }
1659    
1660          sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index          sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1661          sops[0].sem_op = 0;                                                      // check if unlocked          sops[0].sem_op = 0;                                                                // wait until unlocked
1662          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
1663    
1664          sops[1].sem_num = (unsigned short)index + 1; // w_sem of section index          sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1665          sops[1].sem_op = 1;                                                      // lock          sops[1].sem_op = 1;                                                                // lock
1666          sops[1].sem_flg = SEM_UNDO;                                      // undo on terminate          sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1667    
1668          sops[2].sem_num = (unsigned short)index; // r_sem of section index          sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1669          sops[1].sem_op = 0;                                              // wait until unlocked          sops[2].sem_op = 0;                                                        // wait until unlocked
1670          sops[1].sem_flg = 0;          sops[2].sem_flg = 0;
1671    
1672          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1673          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1674    
1675          ret = semtimedop(section_list_pool_semid, sops, 3, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1676            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1677            {
1678                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1679            }
1680    
1681          return ret;          return ret;
1682  }  }
# Line 1269  int section_list_try_rw_lock(SECTION_LIS Line 1684  int section_list_try_rw_lock(SECTION_LIS
1684  int section_list_rd_unlock(SECTION_LIST *p_section)  int section_list_rd_unlock(SECTION_LIST *p_section)
1685  {  {
1686          int index;          int index;
1687          struct sembuf sops[1];          struct sembuf sops[2];
1688          int ret;          int ret;
1689    
1690          index = get_section_index(p_section);          index = get_section_index(p_section);
# Line 1278  int section_list_rd_unlock(SECTION_LIST Line 1693  int section_list_rd_unlock(SECTION_LIST
1693                  return -2;                  return -2;
1694          }          }
1695    
1696          sops[0].sem_num = (unsigned short)index; // r_sem of section index          sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1697          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                               // unlock
1698          sops[0].sem_flg = IPC_NOWAIT;                    // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1699    
1700          ret = semop(section_list_pool_semid, sops, 1);          if (index != BBS_max_section)
1701            {
1702                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1703                    sops[1].sem_op = -1;                                     // unlock
1704                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1705            }
1706    
1707            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1708            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1709            {
1710                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1711            }
1712    
1713          return ret;          return ret;
1714  }  }
# Line 1299  int section_list_rw_unlock(SECTION_LIST Line 1725  int section_list_rw_unlock(SECTION_LIST
1725                  return -2;                  return -2;
1726          }          }
1727    
1728          sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index          sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1729          sops[0].sem_op = -1;                                             // unlock          sops[0].sem_op = -1;                                                       // unlock
1730          sops[0].sem_flg = IPC_NOWAIT;                            // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1731    
1732            ret = semop(p_section_list_pool->semid, sops, 1);
1733            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1734            {
1735                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1736            }
1737    
1738            return ret;
1739    }
1740    
1741    int section_list_rd_lock(SECTION_LIST *p_section)
1742    {
1743            int timer = 0;
1744            int sid = (p_section == NULL ? 0 : p_section->sid);
1745            int ret = -1;
1746    
1747            while (!SYS_server_exit)
1748            {
1749                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1750                    if (ret == 0) // success
1751                    {
1752                            break;
1753                    }
1754                    else if (errno == EAGAIN || errno == EINTR) // retry
1755                    {
1756                            timer++;
1757                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1758                            {
1759                                    log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid);
1760                            }
1761                    }
1762                    else // failed
1763                    {
1764                            log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1765                            break;
1766                    }
1767            }
1768    
1769            return ret;
1770    }
1771    
1772          ret = semop(section_list_pool_semid, sops, 1);  int section_list_rw_lock(SECTION_LIST *p_section)
1773    {
1774            int timer = 0;
1775            int sid = (p_section == NULL ? 0 : p_section->sid);
1776            int ret = -1;
1777    
1778            while (!SYS_server_exit)
1779            {
1780                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1781                    if (ret == 0) // success
1782                    {
1783                            break;
1784                    }
1785                    else if (errno == EAGAIN || errno == EINTR) // retry
1786                    {
1787                            timer++;
1788                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1789                            {
1790                                    log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid);
1791                            }
1792                    }
1793                    else // failed
1794                    {
1795                            log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1796                            break;
1797                    }
1798            }
1799    
1800          return ret;          return ret;
1801  }  }


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

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