/[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.20 by sysadm, Sun May 25 08:15:03 2025 UTC Revision 1.54 by sysadm, Wed Nov 5 03:01:14 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                             section_list.c  -  description  /*
3                                                           -------------------   * section_list
4          Copyright            : (C) 2004-2025 by Leaflet   *   - data models and basic operations of section and article
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
 #define _GNU_SOURCE  
   
 #include "section_list.h"  
9  #include "log.h"  #include "log.h"
10    #include "section_list.h"
11  #include "trie_dict.h"  #include "trie_dict.h"
12    #include "user_list.h"
13    #include <errno.h>
14    #include <signal.h>
15  #include <stdio.h>  #include <stdio.h>
16    #include <stdlib.h>
17  #include <string.h>  #include <string.h>
 #include <signal.h>  
18  #include <unistd.h>  #include <unistd.h>
19  #include <stdlib.h>  #include <sys/ipc.h>
 #include <errno.h>  
20  #include <sys/param.h>  #include <sys/param.h>
21  #include <sys/sem.h>  #include <sys/sem.h>
22  #include <sys/shm.h>  #include <sys/shm.h>
 #include <sys/ipc.h>  
23    
24  #ifdef _SEM_SEMUN_UNDEFINED  #ifdef _SEM_SEMUN_UNDEFINED
25  union semun  union semun
# Line 41  union semun Line 32  union semun
32  };  };
33  #endif // #ifdef _SEM_SEMUN_UNDEFINED  #endif // #ifdef _SEM_SEMUN_UNDEFINED
34    
35  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #define SECTION_TRY_LOCK_WAIT_TIME 1 // second
36  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  #define SECTION_TRY_LOCK_TIMES 10
37    
38    #define ARTICLE_BLOCK_PER_SHM 1000               // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
39    #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
40  #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)
41    
42  #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
43    
44  #define SID_STR_LEN 5 // 32-bit + NULL  #define SID_STR_LEN 5 // 32-bit + NULL
45    
46  struct article_block_t  struct article_block_t
47  {  {
48          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[BBS_article_count_per_block];
49          int32_t article_count;          int article_count;
50          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
51  };  };
52  typedef struct article_block_t ARTICLE_BLOCK;  typedef struct article_block_t ARTICLE_BLOCK;
53    
 struct article_block_shm_t  
 {  
         int shmid;  
         void *p_shm;  
 };  
 typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  
   
54  struct article_block_pool_t  struct article_block_pool_t
55  {  {
56          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          int shmid;
57            struct
58            {
59                    int shmid;
60                    void *p_shm;
61            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
62          int shm_count;          int shm_count;
63          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
64          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
65          int32_t block_count;          int block_count;
66  };  };
67  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
68    
 static int article_block_pool_shmid;  
69  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
70    
71  static int section_list_pool_semid;  SECTION_LIST_POOL *p_section_list_pool = NULL;
 static int section_list_pool_shmid;  
 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;  
72    
73  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
74  {  {
# Line 103  int article_block_init(const char *filen Line 88  int article_block_init(const char *filen
88                  return -1;                  return -1;
89          }          }
90    
91          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
92          {          {
93                  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);
94                  return -2;                  return -2;
# Line 132  int article_block_init(const char *filen Line 117  int article_block_init(const char *filen
117                  return -3;                  return -3;
118          }          }
119    
         article_block_pool_shmid = shmid;  
120          p_article_block_pool = p_shm;          p_article_block_pool = p_shm;
121            p_article_block_pool->shmid = shmid;
122    
123          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
124          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
# Line 196  int article_block_init(const char *filen Line 181  int article_block_init(const char *filen
181    
182  void article_block_cleanup(void)  void article_block_cleanup(void)
183  {  {
184            int shmid;
185    
186          if (p_article_block_pool == NULL)          if (p_article_block_pool == NULL)
187          {          {
188                  return;                  return;
# Line 214  void article_block_cleanup(void) Line 201  void article_block_cleanup(void)
201                  }                  }
202          }          }
203    
204            shmid = p_article_block_pool->shmid;
205    
206          if (shmdt(p_article_block_pool) == -1)          if (shmdt(p_article_block_pool) == -1)
207          {          {
208                  log_error("shmdt(shmid = %d) error (%d)\n", article_block_pool_shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
209            }
210    
211            if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
212            {
213                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
214            }
215    
216            p_article_block_pool = NULL;
217    }
218    
219    int set_article_block_shm_readonly(void)
220    {
221            int shmid;
222            void *p_shm;
223            int i;
224    
225            if (p_article_block_pool == NULL)
226            {
227                    log_error("article_block_pool not initialized\n");
228                    return -1;
229            }
230    
231            for (i = 0; i < p_article_block_pool->shm_count; i++)
232            {
233                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
234    
235                    // Remap shared memory in read-only mode
236                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
237                    if (p_shm == (void *)-1)
238                    {
239                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
240                            return -2;
241                    }
242            }
243    
244            return 0;
245    }
246    
247    int detach_article_block_shm(void)
248    {
249            int shmid;
250    
251            if (p_article_block_pool == NULL)
252            {
253                    return -1;
254            }
255    
256            for (int i = 0; i < p_article_block_pool->shm_count; i++)
257            {
258                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
259                    {
260                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
261                            return -2;
262                    }
263          }          }
264    
265          if (shmctl(article_block_pool_shmid, IPC_RMID, NULL) == -1)          shmid = p_article_block_pool->shmid;
266    
267            if (shmdt(p_article_block_pool) == -1)
268          {          {
269                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", article_block_pool_shmid, errno);                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
270                    return -3;
271          }          }
272    
273          p_article_block_pool = NULL;          p_article_block_pool = NULL;
274    
275            return 0;
276  }  }
277    
278  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 287  ARTICLE *article_block_find_by_aid(int32 Line 335  ARTICLE *article_block_find_by_aid(int32
335          }          }
336    
337          left = 0;          left = 0;
338          right = p_article_block_pool->block_count;          right = p_article_block_pool->block_count - 1;
339    
340          // 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] ]
341          while (left < right - 1)          while (left < right)
342          {          {
343                  // 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
344                  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;  
                 }  
345    
346                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)                  if (aid < p_article_block_pool->p_block[mid]->articles[0].aid)
347                  {                  {
348                          right = mid;                          right = mid - 1;
349                  }                  }
350                  else                  else // if (aid >= p_article_block_pool->p_block[mid]->articles[0].aid)
351                  {                  {
352                          left = mid;                          left = mid;
353                  }                  }
# Line 331  ARTICLE *article_block_find_by_aid(int32 Line 373  ARTICLE *article_block_find_by_aid(int32
373                  }                  }
374          }          }
375    
376          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
377            {
378                    return NULL;
379            }
380    
381            return (p_block->articles + left); // found
382  }  }
383    
384  ARTICLE *article_block_find_by_index(int index)  ARTICLE *article_block_find_by_index(int index)
# Line 344  ARTICLE *article_block_find_by_index(int Line 391  ARTICLE *article_block_find_by_index(int
391                  return NULL;                  return NULL;
392          }          }
393    
394          if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count)          if (index < 0 || index / BBS_article_count_per_block >= p_article_block_pool->block_count)
395          {          {
396                  log_error("article_block_find_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);
397                  return NULL;                  return NULL;
398          }          }
399    
400          p_block = p_article_block_pool->p_block[index / ARTICLE_PER_BLOCK];          p_block = p_article_block_pool->p_block[index / BBS_article_count_per_block];
401    
402          if (index % ARTICLE_PER_BLOCK >= p_block->article_count)          if (index % BBS_article_count_per_block >= p_block->article_count)
403          {          {
404                  log_error("article_block_find_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);
405                  return NULL;                  return NULL;
406          }          }
407    
408          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % BBS_article_count_per_block));
409  }  }
410    
411  extern int section_list_init(const char *filename)  extern int section_list_init(const char *filename)
# Line 372  extern int section_list_init(const char Line 419  extern int section_list_init(const char
419          union semun arg;          union semun arg;
420          int i;          int i;
421    
422          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)
423          {          {
424                  section_list_cleanup();                  section_list_cleanup();
425          }          }
# Line 385  extern int section_list_init(const char Line 432  extern int section_list_init(const char
432                  return -3;                  return -3;
433          }          }
434    
435            // Allocate shared memory
436            size = sizeof(SECTION_LIST_POOL);
437            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
438            if (shmid == -1)
439            {
440                    log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);
441                    return -3;
442            }
443            p_shm = shmat(shmid, NULL, 0);
444            if (p_shm == (void *)-1)
445            {
446                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
447                    return -3;
448            }
449    
450            p_section_list_pool = p_shm;
451            p_section_list_pool->shmid = shmid;
452            p_section_list_pool->section_count = 0;
453    
454            // Allocate semaphore as section locks
455          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
456          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
457          if (semid == -1)          if (semid == -1)
# Line 404  extern int section_list_init(const char Line 471  extern int section_list_init(const char
471                  }                  }
472          }          }
473    
474          section_list_pool_semid = semid;          p_section_list_pool->semid = semid;
475    
476          size = sizeof(SECTION_LIST) * BBS_max_section;          p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
477          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
         if (shmid == -1)  
478          {          {
479                  log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
480                  return -3;                  return -2;
481          }          }
482          p_shm = shmat(shmid, NULL, 0);  
483            p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
484            if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
485            {
486                    log_error("trie_dict_create() OOM\n", BBS_max_section);
487                    return -2;
488            }
489    
490            return 0;
491    }
492    
493    void section_list_cleanup(void)
494    {
495            int shmid;
496    
497            if (p_section_list_pool == NULL)
498            {
499                    return;
500            }
501    
502            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
503            {
504                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
505                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
506            }
507    
508            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
509            {
510                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
511                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
512            }
513    
514            shmid = p_section_list_pool->shmid;
515    
516            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
517            {
518                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
519            }
520    
521            if (shmdt(p_section_list_pool) == -1)
522            {
523                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
524            }
525    
526            if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
527            {
528                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
529            }
530    
531            p_section_list_pool = NULL;
532    }
533    
534    int set_section_list_shm_readonly(void)
535    {
536            int shmid;
537            void *p_shm;
538    
539            if (p_section_list_pool == NULL)
540            {
541                    log_error("p_section_list_pool not initialized\n");
542                    return -1;
543            }
544    
545            shmid = p_section_list_pool->shmid;
546    
547            // Remap shared memory in read-only mode
548            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
549          if (p_shm == (void *)-1)          if (p_shm == (void *)-1)
550          {          {
551                  log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);                  log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
552                  return -3;                  return -3;
553          }          }
554    
         section_list_pool_shmid = shmid;  
555          p_section_list_pool = p_shm;          p_section_list_pool = p_shm;
         section_list_count = 0;  
556    
557          p_trie_dict_section_by_name = trie_dict_create();          return 0;
558          if (p_trie_dict_section_by_name == NULL)  }
         {  
                 log_error("trie_dict_create() OOM\n", BBS_max_section);  
                 return -2;  
         }  
559    
560          p_trie_dict_section_by_sid = trie_dict_create();  int detach_section_list_shm(void)
561          if (p_trie_dict_section_by_sid == NULL)  {
562            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
563          {          {
564                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("shmdt(section_list_pool) error (%d)\n", errno);
565                  return -2;                  return -1;
566          }          }
567    
568            p_section_list_pool = NULL;
569    
570          return 0;          return 0;
571  }  }
572    
# Line 455  inline static void sid_to_str(int32_t si Line 584  inline static void sid_to_str(int32_t si
584          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
585  }  }
586    
587  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)
588  {  {
589          SECTION_LIST *p_section;          SECTION_LIST *p_section;
590          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
591    
592          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)
593          {          {
594                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized\n");
595                  return NULL;                  return NULL;
596          }          }
597    
598          if (section_list_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
599          {          {
600                  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);
601                  return NULL;                  return NULL;
602          }          }
603    
604          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
605    
606          p_section = p_section_list_pool + section_list_count;          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
607    
608          p_section->sid = sid;          p_section->sid = sid;
609            p_section->ex_menu_tm = 0;
610    
611          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
612          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
613    
614          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
615          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
616    
617          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);
618          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
619    
620          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)
621          {          {
622                  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);
623                  return NULL;                  return NULL;
624          }          }
625    
626          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)
627          {          {
628                  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);
629                  return NULL;                  return NULL;
630          }          }
631    
632          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
633    
634          section_list_count++;          p_section_list_pool->section_count++;
635    
636          return p_section;          return p_section;
637  }  }
638    
639    int section_list_update(SECTION_LIST *p_section, const char *sname, const char *stitle, const char *master_list)
640    {
641            int64_t index;
642    
643            if (p_section == NULL || sname == NULL || stitle == NULL || master_list == NULL)
644            {
645                    log_error("NULL pointer error\n");
646                    return -1;
647            }
648    
649            index = get_section_index(p_section);
650    
651            strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
652            p_section->sname[sizeof(p_section->sname) - 1] = '\0';
653    
654            strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
655            p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
656    
657            strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1);
658            p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
659    
660            if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, index) < 0)
661            {
662                    log_error("trie_dict_set(section, %s, %d) error\n", sname, index);
663                    return -2;
664            }
665    
666            return 0;
667    }
668    
669  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
670  {  {
671          p_section->article_count = 0;          p_section->article_count = 0;
# Line 517  void section_list_reset_articles(SECTION Line 677  void section_list_reset_articles(SECTION
677    
678          p_section->page_count = 0;          p_section->page_count = 0;
679          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
 }  
   
 void section_list_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;  
         }  
   
         if (p_section_list_pool != NULL)  
         {  
                 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);  
                 }  
         }  
680    
681          section_list_count = 0;          p_section->ontop_article_count = 0;
682  }  }
683    
684  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
685  {  {
686          int64_t index;          int64_t index;
687            int ret;
688    
689          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL)          if (p_section_list_pool == NULL)
690          {          {
691                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
692                  return NULL;                  return NULL;
693          }          }
694    
695          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);
696            if (ret < 0)
697          {          {
698                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
699                  return NULL;                  return NULL;
700          }          }
701            else if (ret == 0)
702            {
703                    return NULL;
704            }
705    
706          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
707  }  }
708    
709  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
710  {  {
711          int64_t index;          int64_t index;
712            int ret;
713          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
714    
715          if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL)
716          {          {
717                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
718                  return NULL;                  return NULL;
# Line 587  SECTION_LIST *section_list_find_by_sid(i Line 720  SECTION_LIST *section_list_find_by_sid(i
720    
721          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
722    
723          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);
724            if (ret < 0)
725          {          {
726                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
727                  return NULL;                  return NULL;
728          }          }
729            else if (ret == 0)
730            {
731                    return NULL;
732            }
733    
734          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
735  }  }
736    
737  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 606  int section_list_append_article(SECTION_ Line 744  int section_list_append_article(SECTION_
744    
745          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
746          {          {
747                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
748                  return -1;                  return -1;
749          }          }
750    
# Line 629  int section_list_append_article(SECTION_ Line 767  int section_list_append_article(SECTION_
767          }          }
768    
769          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
770                  p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK)                  p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= BBS_article_count_per_block)
771          {          {
772                  if ((p_block = pop_free_article_block()) == NULL)                  if ((p_block = pop_free_article_block()) == NULL)
773                  {                  {
# Line 639  int section_list_append_article(SECTION_ Line 777  int section_list_append_article(SECTION_
777    
778                  if (p_article_block_pool->block_count > 0)                  if (p_article_block_pool->block_count > 0)
779                  {                  {
780                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[ARTICLE_PER_BLOCK - 1].aid;                          last_aid = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->articles[BBS_article_count_per_block - 1].aid;
781                  }                  }
782    
783                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;                  p_article_block_pool->p_block[p_article_block_pool->block_count] = p_block;
# Line 718  int section_list_append_article(SECTION_ Line 856  int section_list_append_article(SECTION_
856          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
857    
858          // Update page          // Update page
859          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) ||
860                  p_section->article_count == 1)                  p_section->page_count == 0)
861          {          {
862                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
863                  p_section->page_count++;                  p_section->page_count++;
# Line 731  int section_list_append_article(SECTION_ Line 869  int section_list_append_article(SECTION_
869                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
870          }          }
871    
872            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
873            {
874                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
875                                      p_section->sid, p_article->aid);
876                    return -5;
877            }
878    
879          return 0;          return 0;
880  }  }
881    
# Line 742  int section_list_set_article_visible(SEC Line 887  int section_list_set_article_visible(SEC
887    
888          if (p_section == NULL)          if (p_section == NULL)
889          {          {
890                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
891                  return -2;                  return -1;
892          }          }
893    
894          p_article = article_block_find_by_aid(aid);          p_article = article_block_find_by_aid(aid);
# Line 754  int section_list_set_article_visible(SEC Line 899  int section_list_set_article_visible(SEC
899    
900          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
901          {          {
902                  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);
903                  return -2;                  return -2;
904          }          }
905    
# Line 767  int section_list_set_article_visible(SEC Line 912  int section_list_set_article_visible(SEC
912          {          {
913                  p_section->visible_article_count--;                  p_section->visible_article_count--;
914    
915                    if (user_article_cnt_inc(p_article->uid, -1) < 0)
916                    {
917                            log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_article->uid);
918                    }
919    
920                  if (p_article->tid == 0)                  if (p_article->tid == 0)
921                  {                  {
922                          p_section->visible_topic_count--;                          p_section->visible_topic_count--;
# Line 785  int section_list_set_article_visible(SEC Line 935  int section_list_set_article_visible(SEC
935                                          p_reply->visible = 0;                                          p_reply->visible = 0;
936                                          p_section->visible_article_count--;                                          p_section->visible_article_count--;
937                                          affected_count++;                                          affected_count++;
938    
939                                            if (user_article_cnt_inc(p_reply->uid, -1) < 0)
940                                            {
941                                                    log_error("user_article_cnt_inc(uid=%d, -1) error\n", p_reply->uid);
942                                            }
943                                  }                                  }
944                          }                          }
945                  }                  }
# Line 797  int section_list_set_article_visible(SEC Line 952  int section_list_set_article_visible(SEC
952                  {                  {
953                          p_section->visible_topic_count++;                          p_section->visible_topic_count++;
954                  }                  }
955    
956                    if (user_article_cnt_inc(p_article->uid, 1) < 0)
957                    {
958                            log_error("user_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
959                    }
960          }          }
961    
962          p_article->visible = visible;          p_article->visible = visible;
# Line 805  int section_list_set_article_visible(SEC Line 965  int section_list_set_article_visible(SEC
965          return affected_count;          return affected_count;
966  }  }
967    
968    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
969    {
970            int i;
971    
972            if (p_section == NULL || p_article == NULL)
973            {
974                    log_error("NULL pointer error\n");
975                    return -1;
976            }
977    
978            if (p_section->sid != p_article->sid)
979            {
980                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
981                    return -2;
982            }
983    
984            if (p_article->ontop)
985            {
986                    for (i = 0; i < p_section->ontop_article_count; i++)
987                    {
988                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
989                            {
990                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
991                                    return 0;
992                            }
993                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
994                            {
995                                    break;
996                            }
997                    }
998    
999                    // Remove the oldest one if the array of ontop articles is full
1000                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
1001                    {
1002                            if (i == 0) // p_article is the oldest one
1003                            {
1004                                    return 0;
1005                            }
1006                            memmove((void *)(p_section->p_ontop_articles),
1007                                            (void *)(p_section->p_ontop_articles + 1),
1008                                            sizeof(ARTICLE *) * (size_t)(i - 1));
1009                            p_section->ontop_article_count--;
1010                            i--;
1011                    }
1012                    else
1013                    {
1014                            memmove((void *)(p_section->p_ontop_articles + i + 1),
1015                                            (void *)(p_section->p_ontop_articles + i),
1016                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
1017                    }
1018    
1019                    p_section->p_ontop_articles[i] = p_article;
1020                    p_section->ontop_article_count++;
1021    
1022                    // TODO: debug
1023            }
1024            else // ontop == 0
1025            {
1026                    for (i = 0; i < p_section->ontop_article_count; i++)
1027                    {
1028                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
1029                            {
1030                                    break;
1031                            }
1032                    }
1033                    if (i == p_section->ontop_article_count) // not found
1034                    {
1035                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1036                            return 0;
1037                    }
1038    
1039                    memmove((void *)(p_section->p_ontop_articles + i),
1040                                    (void *)(p_section->p_ontop_articles + i + 1),
1041                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1042                    p_section->ontop_article_count--;
1043            }
1044    
1045            return 0;
1046    }
1047    
1048    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1049    {
1050            int page_count;
1051    
1052            if (p_section == NULL)
1053            {
1054                    log_error("NULL pointer error\n");
1055                    return -1;
1056            }
1057    
1058            page_count = p_section->page_count - 1 +
1059                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1060                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page ? 1 : 0);
1061    
1062            if (page_count < 0)
1063            {
1064                    page_count = 0;
1065            }
1066    
1067            return page_count;
1068    }
1069    
1070    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1071    {
1072            if (p_section == NULL)
1073            {
1074                    log_error("NULL pointer error\n");
1075                    return -1;
1076            }
1077    
1078            if (page_id < p_section->page_count - 1)
1079            {
1080                    return BBS_article_limit_per_page;
1081            }
1082            else // if (page_id >= p_section->page_count - 1)
1083            {
1084                    return MIN(MAX(0,
1085                                               (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1086                                                    BBS_article_limit_per_page * (page_id - p_section->page_count + 1))),
1087                                       BBS_article_limit_per_page);
1088            }
1089    }
1090    
1091  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)
1092  {  {
1093          ARTICLE *p_article;          ARTICLE *p_article;
# Line 818  ARTICLE *section_list_find_article_with_ Line 1101  ARTICLE *section_list_find_article_with_
1101    
1102          if (p_section == NULL)          if (p_section == NULL)
1103          {          {
1104                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1105                  return NULL;                  return NULL;
1106          }          }
1107    
# Line 830  ARTICLE *section_list_find_article_with_ Line 1113  ARTICLE *section_list_find_article_with_
1113          }          }
1114    
1115          left = 0;          left = 0;
1116          right = p_section->page_count;          right = p_section->page_count - 1;
1117    
1118          // 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] ]
1119          while (left < right - 1)          while (left < right)
1120          {          {
1121                  // 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
1122                  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;  
                 }  
1123    
1124                  if (aid < p_section->p_page_first_article[mid]->aid)                  if (aid < p_section->p_page_first_article[mid]->aid)
1125                  {                  {
1126                          right = mid;                          right = mid - 1;
1127                  }                  }
1128                  else                  else // if (aid < p_section->p_page_first_article[mid]->aid)
1129                  {                  {
1130                          left = mid;                          left = mid;
1131                  }                  }
# Line 910  int section_list_calculate_page(SECTION_ Line 1187  int section_list_calculate_page(SECTION_
1187    
1188          if (p_section == NULL)          if (p_section == NULL)
1189          {          {
1190                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1191                  return -1;                  return -1;
1192          }          }
1193    
# Line 999  int section_list_calculate_page(SECTION_ Line 1276  int section_list_calculate_page(SECTION_
1276          } while (p_article != p_section->p_article_head);          } while (p_article != p_section->p_article_head);
1277    
1278          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);          p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
1279          p_section->last_page_visible_article_count = visible_article_count;          p_section->last_page_visible_article_count = (visible_article_count > 0
1280                                                                                                              ? visible_article_count
1281                                                                                                              : (page > 0 ? BBS_article_limit_per_page : 0));
1282    
1283          return 0;          return 0;
1284  }  }
1285    
1286    int32_t article_block_last_aid(void)
1287    {
1288            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1289            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1290    
1291            return last_aid;
1292    }
1293    
1294    int article_block_article_count(void)
1295    {
1296            int ret;
1297    
1298            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1299            {
1300                    return -1;
1301            }
1302    
1303            ret = (p_article_block_pool->block_count - 1) * BBS_article_count_per_block +
1304                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1305    
1306            return ret;
1307    }
1308    
1309  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1310  {  {
1311          ARTICLE *p_article;          ARTICLE *p_article;
# Line 1044  int section_list_move_topic(SECTION_LIST Line 1346  int section_list_move_topic(SECTION_LIST
1346          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1347          int move_counter;          int move_counter;
1348    
1349          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1350          {          {
1351                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("NULL pointer error\n");
1352                    return -1;
1353            }
1354    
1355            if (p_section_src->sid == p_section_dest->sid)
1356            {
1357                    log_error("section_list_move_topic() src and dest section are the same\n");
1358                  return -1;                  return -1;
1359          }          }
1360    
1361          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1362          {          {
1363                  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);
1364                  return -2;                  return -2;
1365          }          }
1366    
# Line 1074  int section_list_move_topic(SECTION_LIST Line 1382  int section_list_move_topic(SECTION_LIST
1382          move_article_count = article_count_of_topic(aid);          move_article_count = article_count_of_topic(aid);
1383          if (move_article_count <= 0)          if (move_article_count <= 0)
1384          {          {
1385                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1386                  return -2;                  return -2;
1387          }          }
1388    
# Line 1093  int section_list_move_topic(SECTION_LIST Line 1401  int section_list_move_topic(SECTION_LIST
1401          {          {
1402                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1403                  {                  {
1404                          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",
1405                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1406                          return -2;                          p_article = p_article->p_topic_next;
1407                            continue;
1408                  }                  }
1409    
1410                  // Remove from bi-directional article list of src section                  // Remove from bi-directional article list of src section
# Line 1121  int section_list_move_topic(SECTION_LIST Line 1430  int section_list_move_topic(SECTION_LIST
1430    
1431                  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)
1432                  {                  {
1433                          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);
1434                          return -4;                          p_article = p_article->p_topic_next;
1435                            continue;
1436                  }                  }
1437    
1438                  // Insert into bi-directional article list of dest section                  // Insert into bi-directional article list of dest section
# Line 1200  int section_list_move_topic(SECTION_LIST Line 1510  int section_list_move_topic(SECTION_LIST
1510    
1511          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)
1512          {          {
1513                  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",
1514                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1515          }          }
1516    
# Line 1240  int get_section_index(SECTION_LIST *p_se Line 1550  int get_section_index(SECTION_LIST *p_se
1550          }          }
1551          else          else
1552          {          {
1553                  index = (int)(p_section - p_section_list_pool);                  index = (int)(p_section - p_section_list_pool->sections);
1554                  if (index < 0 || index >= BBS_max_section)                  if (index < 0 || index >= BBS_max_section)
1555                  {                  {
1556                          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 1251  int get_section_index(SECTION_LIST *p_se Line 1561  int get_section_index(SECTION_LIST *p_se
1561          return index;          return index;
1562  }  }
1563    
1564    int get_section_info(SECTION_LIST *p_section, char *sname, char *stitle, char *master_list)
1565    {
1566            if (p_section == NULL)
1567            {
1568                    log_error("NULL pointer error\n");
1569                    return -1;
1570            }
1571    
1572            if (section_list_rd_lock(p_section) < 0)
1573            {
1574                    log_error("section_list_rd_lock(sid=%d) error\n", p_section->sid);
1575                    return -2;
1576            }
1577    
1578            if (sname != NULL)
1579            {
1580                    memcpy(sname, p_section->sname, sizeof(p_section->sname));
1581            }
1582            if (stitle != NULL)
1583            {
1584                    memcpy(stitle, p_section->stitle, sizeof(p_section->stitle));
1585            }
1586            if (master_list != NULL)
1587            {
1588                    memcpy(master_list, p_section->master_list, sizeof(p_section->master_list));
1589            }
1590    
1591            // release lock of section
1592            if (section_list_rd_unlock(p_section) < 0)
1593            {
1594                    log_error("section_list_rd_unlock(sid=%d) error\n", p_section->sid);
1595                    return -2;
1596            }
1597    
1598            return 0;
1599    }
1600    
1601  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)
1602  {  {
1603          int index;          int index;
# Line 1275  int section_list_try_rd_lock(SECTION_LIS Line 1622  int section_list_try_rd_lock(SECTION_LIS
1622          // Read lock on any specific section will also acquire single read lock on "all section"          // Read lock on any specific section will also acquire single read lock on "all section"
1623          // so that write lock on all section only need to acquire single write on on "all section"          // so that write lock on all section only need to acquire single write on on "all section"
1624          // rather than to acquire multiple write locks on all the available sections.          // rather than to acquire multiple write locks on all the available sections.
1625          if (index == BBS_max_section)          if (index != BBS_max_section)
1626          {          {
1627                  sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section                  sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1628                  sops[2].sem_op = 0;                                                // wait until unlocked                  sops[2].sem_op = 0;                                                // wait until unlocked
# Line 1289  int section_list_try_rd_lock(SECTION_LIS Line 1636  int section_list_try_rd_lock(SECTION_LIS
1636          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1637          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1638    
1639          ret = semtimedop(section_list_pool_semid, sops, (index == BBS_max_section ? 4 : 2), &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1640          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1641          {          {
1642                  log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);                  log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
# Line 1326  int section_list_try_rw_lock(SECTION_LIS Line 1673  int section_list_try_rw_lock(SECTION_LIS
1673          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1674          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1675    
1676          ret = semtimedop(section_list_pool_semid, sops, 3, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1677          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1678          {          {
1679                  log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);                  log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
# Line 1351  int section_list_rd_unlock(SECTION_LIST Line 1698  int section_list_rd_unlock(SECTION_LIST
1698          sops[0].sem_op = -1;                                               // unlock          sops[0].sem_op = -1;                                               // unlock
1699          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1700    
1701          // The same reason as section_list_try_rd_lock()          if (index != BBS_max_section)
         if (index == BBS_max_section)  
1702          {          {
1703                  sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section                  sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1704                  sops[1].sem_op = -1;                                     // unlock                  sops[1].sem_op = -1;                                     // unlock
1705                  sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait                  sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1706          }          }
1707    
1708          ret = semop(section_list_pool_semid, sops, (index == BBS_max_section ? 2 : 1));          ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1709          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1710          {          {
1711                  log_error("semop(index = %d, unlock read) error %d\n", index, errno);                  log_error("semop(index = %d, unlock read) error %d\n", index, errno);
# Line 1384  int section_list_rw_unlock(SECTION_LIST Line 1730  int section_list_rw_unlock(SECTION_LIST
1730          sops[0].sem_op = -1;                                                       // unlock          sops[0].sem_op = -1;                                                       // unlock
1731          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1732    
1733          ret = semop(section_list_pool_semid, sops, 1);          ret = semop(p_section_list_pool->semid, sops, 1);
1734          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
1735          {          {
1736                  log_error("semop(index = %d, unlock write) error %d\n", index, errno);                  log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1737          }          }
1738    
1739          return ret;          return ret;
1740    }
1741    
1742    int section_list_rd_lock(SECTION_LIST *p_section)
1743    {
1744            int timer = 0;
1745            int sid = (p_section == NULL ? 0 : p_section->sid);
1746            int ret = -1;
1747    
1748            while (!SYS_server_exit)
1749            {
1750                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1751                    if (ret == 0) // success
1752                    {
1753                            break;
1754                    }
1755                    else if (errno == EAGAIN || errno == EINTR) // retry
1756                    {
1757                            timer++;
1758                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1759                            {
1760                                    log_error("section_list_try_rd_lock() tried %d times on section %d\n", timer, sid);
1761                            }
1762                    }
1763                    else // failed
1764                    {
1765                            log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1766                            break;
1767                    }
1768            }
1769    
1770            return ret;
1771    }
1772    
1773    int section_list_rw_lock(SECTION_LIST *p_section)
1774    {
1775            int timer = 0;
1776            int sid = (p_section == NULL ? 0 : p_section->sid);
1777            int ret = -1;
1778    
1779            while (!SYS_server_exit)
1780            {
1781                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1782                    if (ret == 0) // success
1783                    {
1784                            break;
1785                    }
1786                    else if (errno == EAGAIN || errno == EINTR) // retry
1787                    {
1788                            timer++;
1789                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1790                            {
1791                                    log_error("section_list_try_rw_lock() tried %d times on section %d\n", timer, sid);
1792                            }
1793                    }
1794                    else // failed
1795                    {
1796                            log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1797                            break;
1798                    }
1799            }
1800    
1801            return ret;
1802  }  }


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

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