/[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.42 by sysadm, Tue Oct 14 02:39:34 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 <errno.h>
21    #include <signal.h>
22  #include <stdio.h>  #include <stdio.h>
23    #include <stdlib.h>
24  #include <string.h>  #include <string.h>
 #include <signal.h>  
25  #include <unistd.h>  #include <unistd.h>
26  #include <stdlib.h>  #include <sys/ipc.h>
 #include <errno.h>  
27  #include <sys/param.h>  #include <sys/param.h>
28  #include <sys/sem.h>  #include <sys/sem.h>
29  #include <sys/shm.h>  #include <sys/shm.h>
 #include <sys/ipc.h>  
30    
31  #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
32  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  union semun
33    {
34            int val;                           /* Value for SETVAL */
35            struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
36            unsigned short *array; /* Array for GETALL, SETALL */
37            struct seminfo *__buf; /* Buffer for IPC_INFO
38                                                              (Linux-specific) */
39    };
40    #endif // #ifdef _SEM_SEMUN_UNDEFINED
41    
42    #define SECTION_TRY_LOCK_WAIT_TIME 1 // second
43    #define SECTION_TRY_LOCK_TIMES 10
44    
45    #define ARTICLE_BLOCK_PER_SHM 1000               // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
46    #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 80 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
47  #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)
48    
49  #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
50    
51  #define SID_STR_LEN 5 // 32-bit + NULL  #define SID_STR_LEN 5 // 32-bit + NULL
52    
53  struct article_block_t  struct article_block_t
54  {  {
55          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[ARTICLE_PER_BLOCK];
56          int32_t article_count;          int article_count;
57          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
58  };  };
59  typedef struct article_block_t ARTICLE_BLOCK;  typedef struct article_block_t ARTICLE_BLOCK;
60    
 struct article_block_shm_t  
 {  
         int shmid;  
         void *p_shm;  
 };  
 typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  
   
61  struct article_block_pool_t  struct article_block_pool_t
62  {  {
63          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          int shmid;
64            struct
65            {
66                    int shmid;
67                    void *p_shm;
68            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
69          int shm_count;          int shm_count;
70          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
71          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
72          int32_t block_count;          int block_count;
73  };  };
74  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
75    
76  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
77    
78  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;  
79    
80  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
81  {  {
# Line 90  int article_block_init(const char *filen Line 95  int article_block_init(const char *filen
95                  return -1;                  return -1;
96          }          }
97    
98          if (block_count > ARTICLE_BLOCK_PER_POOL)          if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL)
99          {          {
100                  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);
101                  return -2;                  return -2;
102          }          }
103    
104          p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL));          // Allocate shared memory
105          if (p_article_block_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
106            key = ftok(filename, proj_id);
107            if (key == -1)
108          {          {
109                  log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n");                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
110                  return -2;                  return -3;
111          }          }
112    
113          // Allocate shared memory          size = sizeof(ARTICLE_BLOCK_POOL);
114            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
115            if (shmid == -1)
116            {
117                    log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
118                    return -3;
119            }
120            p_shm = shmat(shmid, NULL, 0);
121            if (p_shm == (void *)-1)
122            {
123                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
124                    return -3;
125            }
126    
127            p_article_block_pool = p_shm;
128            p_article_block_pool->shmid = shmid;
129    
130          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
131          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
132    
# Line 112  int article_block_init(const char *filen Line 135  int article_block_init(const char *filen
135                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
136                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
137    
138                  proj_id = getpid() + p_article_block_pool->shm_count;                  proj_id = p_article_block_pool->shm_count;
139                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
140                  if (key == -1)                  if (key == -1)
141                  {                  {
# Line 121  int article_block_init(const char *filen Line 144  int article_block_init(const char *filen
144                          return -3;                          return -3;
145                  }                  }
146    
147                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
148                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
149                  if (shmid == -1)                  if (shmid == -1)
150                  {                  {
# Line 165  int article_block_init(const char *filen Line 188  int article_block_init(const char *filen
188    
189  void article_block_cleanup(void)  void article_block_cleanup(void)
190  {  {
191          if (p_article_block_pool != NULL)          int shmid;
192    
193            if (p_article_block_pool == NULL)
194            {
195                    return;
196            }
197    
198            for (int i = 0; i < p_article_block_pool->shm_count; i++)
199          {          {
200                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
201                  {                  {
202                          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);
203                          {                  }
                                 log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);  
                         }  
204    
205                          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)
206                          {                  {
207                                  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);
208                          }                  }
209            }
210    
211            shmid = p_article_block_pool->shmid;
212    
213            if (shmdt(p_article_block_pool) == -1)
214            {
215                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
216            }
217    
218            if (shmctl(shmid, IPC_RMID, NULL) == -1)
219            {
220                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
221            }
222    
223            p_article_block_pool = NULL;
224    }
225    
226    int set_article_block_shm_readonly(void)
227    {
228            int shmid;
229            void *p_shm;
230            int i;
231    
232            if (p_article_block_pool == NULL)
233            {
234                    log_error("article_block_pool not initialized\n");
235                    return -1;
236            }
237    
238            for (i = 0; i < p_article_block_pool->shm_count; i++)
239            {
240                    shmid = (p_article_block_pool->shm_pool + i)->shmid;
241    
242                    // Remap shared memory in read-only mode
243                    p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP);
244                    if (p_shm == (void *)-1)
245                    {
246                            log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno);
247                            return -2;
248                    }
249            }
250    
251            return 0;
252    }
253    
254    int detach_article_block_shm(void)
255    {
256            int shmid;
257    
258            if (p_article_block_pool == NULL)
259            {
260                    return -1;
261            }
262    
263            for (int i = 0; i < p_article_block_pool->shm_count; i++)
264            {
265                    if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
266                    {
267                            log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
268                            return -2;
269                  }                  }
270            }
271    
272            shmid = p_article_block_pool->shmid;
273    
274                  free(p_article_block_pool);          if (shmdt(p_article_block_pool) == -1)
275                  p_article_block_pool = NULL;          {
276                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
277                    return -3;
278          }          }
279    
280            p_article_block_pool = NULL;
281    
282            return 0;
283  }  }
284    
285  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 289  ARTICLE *article_block_find_by_aid(int32 Line 386  ARTICLE *article_block_find_by_aid(int32
386                  }                  }
387          }          }
388    
389          return (p_block->articles + left);          if (aid != p_block->articles[left].aid) // not found
390            {
391                    return NULL;
392            }
393    
394            return (p_block->articles + left); // found
395  }  }
396    
397  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 421  ARTICLE *article_block_find_by_index(int
421          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
422  }  }
423    
424  extern int section_list_pool_init(const char *filename)  extern int section_list_init(const char *filename)
425  {  {
426          int semid;          int semid;
427          int shmid;          int shmid;
# Line 327  extern int section_list_pool_init(const Line 429  extern int section_list_pool_init(const
429          key_t key;          key_t key;
430          size_t size;          size_t size;
431          void *p_shm;          void *p_shm;
432            union semun arg;
433            int i;
434    
435          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)  
436          {          {
437                  log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);                  section_list_cleanup();
                 return -1;  
438          }          }
439    
440          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 348  extern int section_list_pool_init(const Line 445  extern int section_list_pool_init(const
445                  return -3;                  return -3;
446          }          }
447    
448          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections          // Allocate shared memory
449          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;  
450          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
451          if (shmid == -1)          if (shmid == -1)
452          {          {
453                  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);
454                  return -3;                  return -3;
455          }          }
456          p_shm = shmat(shmid, NULL, 0);          p_shm = shmat(shmid, NULL, 0);
# Line 372  extern int section_list_pool_init(const Line 460  extern int section_list_pool_init(const
460                  return -3;                  return -3;
461          }          }
462    
         section_list_pool_shmid = shmid;  
463          p_section_list_pool = p_shm;          p_section_list_pool = p_shm;
464          section_list_count = 0;          p_section_list_pool->shmid = shmid;
465            p_section_list_pool->section_count = 0;
466    
467          p_trie_dict_section_by_name = trie_dict_create();          // Allocate semaphore as section locks
468          if (p_trie_dict_section_by_name == NULL)          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
469            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
470            if (semid == -1)
471            {
472                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
473                    return -3;
474            }
475    
476            // Initialize sem value to 0
477            arg.val = 0;
478            for (i = 0; i < size; i++)
479            {
480                    if (semctl(semid, i, SETVAL, arg) == -1)
481                    {
482                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
483                            return -3;
484                    }
485            }
486    
487            p_section_list_pool->semid = semid;
488    
489            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
490            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
491          {          {
492                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
493                  return -2;                  return -2;
494          }          }
495    
496          p_trie_dict_section_by_sid = trie_dict_create();          p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
497          if (p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
498          {          {
499                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
500                  return -2;                  return -2;
# Line 393  extern int section_list_pool_init(const Line 503  extern int section_list_pool_init(const
503          return 0;          return 0;
504  }  }
505    
506    void section_list_cleanup(void)
507    {
508            int shmid;
509    
510            if (p_section_list_pool == NULL)
511            {
512                    return;
513            }
514    
515            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
516            {
517                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
518                    p_section_list_pool->p_trie_dict_section_by_name = NULL;
519            }
520    
521            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
522            {
523                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
524                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
525            }
526    
527            shmid = p_section_list_pool->shmid;
528    
529            if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
530            {
531                    log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
532            }
533    
534            if (shmdt(p_section_list_pool) == -1)
535            {
536                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
537            }
538    
539            if (shmctl(shmid, IPC_RMID, NULL) == -1)
540            {
541                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
542            }
543    
544            p_section_list_pool = NULL;
545    }
546    
547    int set_section_list_shm_readonly(void)
548    {
549            int shmid;
550            void *p_shm;
551    
552            if (p_section_list_pool == NULL)
553            {
554                    log_error("p_section_list_pool not initialized\n");
555                    return -1;
556            }
557    
558            shmid = p_section_list_pool->shmid;
559    
560            // Remap shared memory in read-only mode
561            p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP);
562            if (p_shm == (void *)-1)
563            {
564                    log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno);
565                    return -3;
566            }
567    
568            p_section_list_pool = p_shm;
569    
570            return 0;
571    }
572    
573    int detach_section_list_shm(void)
574    {
575            if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1)
576            {
577                    log_error("shmdt(section_list_pool) error (%d)\n", errno);
578                    return -1;
579            }
580    
581            p_section_list_pool = NULL;
582    
583            return 0;
584    }
585    
586  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)
587  {  {
588          uint32_t u_sid;          uint32_t u_sid;
# Line 407  inline static void sid_to_str(int32_t si Line 597  inline static void sid_to_str(int32_t si
597          p_sid_str[i] = '\0';          p_sid_str[i] = '\0';
598  }  }
599    
600  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)
601  {  {
602          SECTION_LIST *p_section;          SECTION_LIST *p_section;
603          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
604    
605          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)
606          {          {
607                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized\n");
608                  return NULL;                  return NULL;
609          }          }
610    
611          if (section_list_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
612          {          {
613                  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);
614                  return NULL;                  return NULL;
615          }          }
616    
617          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
618    
619          p_section = p_section_list_pool + section_list_count;          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
620    
621          p_section->sid = sid;          p_section->sid = sid;
622            p_section->ex_menu_tm = 0;
623    
624          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1);
625          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname) - 1] = '\0';
626    
627          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1));          strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1);
628          p_section->stitle[sizeof(p_section->stitle - 1)] = '\0';          p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
629    
630          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);
631          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_list[sizeof(p_section->master_list) - 1] = '\0';
632    
633          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)
634          {          {
635                  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);
636                  return NULL;                  return NULL;
637          }          }
638    
639          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)
640          {          {
641                  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]);  
642                  return NULL;                  return NULL;
643          }          }
644    
645          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
646    
647          section_list_count++;          p_section_list_pool->section_count++;
648    
649          return p_section;          return p_section;
650  }  }
# Line 470  void section_list_reset_articles(SECTION Line 660  void section_list_reset_articles(SECTION
660    
661          p_section->page_count = 0;          p_section->page_count = 0;
662          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;  
         }  
663    
664          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;  
665  }  }
666    
667  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
668  {  {
669          int64_t index;          int64_t index;
670            int ret;
671    
672          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL)          if (p_section_list_pool == NULL)
673          {          {
674                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
675                  return NULL;                  return NULL;
676          }          }
677    
678          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);
679            if (ret < 0)
680          {          {
681                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
682                  return NULL;                  return NULL;
683          }          }
684            else if (ret == 0)
685            {
686                    return NULL;
687            }
688    
689          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
690  }  }
691    
692  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
693  {  {
694          int64_t index;          int64_t index;
695            int ret;
696          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
697    
698          if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL)
699          {          {
700                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
701                  return NULL;                  return NULL;
# Line 540  SECTION_LIST *section_list_find_by_sid(i Line 703  SECTION_LIST *section_list_find_by_sid(i
703    
704          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
705    
706          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);
707            if (ret < 0)
708          {          {
709                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
710                  return NULL;                  return NULL;
711          }          }
712            else if (ret == 0)
713            {
714                    return NULL;
715            }
716    
717          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
718  }  }
719    
720  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 727  int section_list_append_article(SECTION_
727    
728          if (p_section == NULL || p_article_src == NULL)          if (p_section == NULL || p_article_src == NULL)
729          {          {
730                  log_error("section_list_append_article() NULL pointer error\n");                  log_error("NULL pointer error\n");
731                  return -1;                  return -1;
732          }          }
733    
# Line 684  int section_list_append_article(SECTION_ Line 852  int section_list_append_article(SECTION_
852                  p_section->last_page_visible_article_count++;                  p_section->last_page_visible_article_count++;
853          }          }
854    
855            if (p_article->ontop && section_list_update_article_ontop(p_section, p_article) < 0)
856            {
857                    log_error("section_list_update_article_ontop(sid=%d, aid=%d) error\n",
858                                      p_section->sid, p_article->aid);
859                    return -5;
860            }
861    
862          return 0;          return 0;
863  }  }
864    
# Line 695  int section_list_set_article_visible(SEC Line 870  int section_list_set_article_visible(SEC
870    
871          if (p_section == NULL)          if (p_section == NULL)
872          {          {
873                  log_error("section_list_set_article_visible() NULL pointer error\n");                  log_error("NULL pointer error\n");
874                  return -2;                  return -1;
875          }          }
876    
877          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 882  int section_list_set_article_visible(SEC
882    
883          if (p_section->sid != p_article->sid)          if (p_section->sid != p_article->sid)
884          {          {
885                  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);
886                  return -2;                  return -2;
887          }          }
888    
# Line 758  int section_list_set_article_visible(SEC Line 933  int section_list_set_article_visible(SEC
933          return affected_count;          return affected_count;
934  }  }
935    
936    int section_list_update_article_ontop(SECTION_LIST *p_section, ARTICLE *p_article)
937    {
938            int i;
939    
940            if (p_section == NULL || p_article == NULL)
941            {
942                    log_error("NULL pointer error\n");
943                    return -1;
944            }
945    
946            if (p_section->sid != p_article->sid)
947            {
948                    log_error("Inconsistent section sid %d != article sid %d\n", p_section->sid, p_article->sid);
949                    return -2;
950            }
951    
952            if (p_article->ontop)
953            {
954                    for (i = 0; i < p_section->ontop_article_count; i++)
955                    {
956                            if (p_section->p_ontop_articles[i]->aid == p_article->aid)
957                            {
958                                    log_error("Inconsistent state found: article %d already ontop in section %d\n", p_article->aid, p_section->sid);
959                                    return 0;
960                            }
961                            else if (p_section->p_ontop_articles[i]->aid > p_article->aid)
962                            {
963                                    break;
964                            }
965                    }
966    
967                    // Remove the oldest one if the array of ontop articles is full
968                    if (p_section->ontop_article_count >= BBS_ontop_article_limit_per_section)
969                    {
970                            if (i == 0) // p_article is the oldest one
971                            {
972                                    return 0;
973                            }
974                            memmove((void *)(p_section->p_ontop_articles),
975                                            (void *)(p_section->p_ontop_articles + 1),
976                                            sizeof(ARTICLE *) * (size_t)(i - 1));
977                            p_section->ontop_article_count--;
978                            i--;
979                    }
980                    else
981                    {
982                            memmove((void *)(p_section->p_ontop_articles + i + 1),
983                                            (void *)(p_section->p_ontop_articles + i),
984                                            sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i));
985                    }
986    
987                    p_section->p_ontop_articles[i] = p_article;
988                    p_section->ontop_article_count++;
989    
990                    // TODO: debug
991            }
992            else // ontop == 0
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                                    break;
999                            }
1000                    }
1001                    if (i == p_section->ontop_article_count) // not found
1002                    {
1003                            log_error("Inconsistent state found: article %d not ontop in section %d\n", p_article->aid, p_section->sid);
1004                            return 0;
1005                    }
1006    
1007                    memmove((void *)(p_section->p_ontop_articles + i),
1008                                    (void *)(p_section->p_ontop_articles + i + 1),
1009                                    sizeof(ARTICLE *) * (size_t)(p_section->ontop_article_count - i - 1));
1010                    p_section->ontop_article_count--;
1011            }
1012    
1013            return 0;
1014    }
1015    
1016    int section_list_page_count_with_ontop(SECTION_LIST *p_section)
1017    {
1018            int page_count;
1019    
1020            if (p_section == NULL)
1021            {
1022                    log_error("NULL pointer error\n");
1023                    return -1;
1024            }
1025    
1026            page_count = p_section->page_count - (p_section->last_page_visible_article_count > 0 ? 1 : 0) +
1027                                     (p_section->last_page_visible_article_count + p_section->ontop_article_count) / BBS_article_limit_per_page +
1028                                     ((p_section->last_page_visible_article_count + p_section->ontop_article_count) % BBS_article_limit_per_page == 0 ? 0 : 1);
1029    
1030            return page_count;
1031    }
1032    
1033    int section_list_page_article_count_with_ontop(SECTION_LIST *p_section, int32_t page_id)
1034    {
1035            if (p_section == NULL)
1036            {
1037                    log_error("NULL pointer error\n");
1038                    return -1;
1039            }
1040    
1041            if (page_id < p_section->page_count - 1)
1042            {
1043                    return BBS_article_limit_per_page;
1044            }
1045            else // if (page_id >= p_section->page_count - 1)
1046            {
1047                    return MAX(0, (p_section->last_page_visible_article_count + p_section->ontop_article_count -
1048                                               BBS_article_limit_per_page * (page_id - p_section->page_count + 1)));
1049            }
1050    }
1051    
1052  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)
1053  {  {
1054          ARTICLE *p_article;          ARTICLE *p_article;
# Line 771  ARTICLE *section_list_find_article_with_ Line 1062  ARTICLE *section_list_find_article_with_
1062    
1063          if (p_section == NULL)          if (p_section == NULL)
1064          {          {
1065                  log_error("section_list_find_article_with_offset() NULL pointer error\n");                  log_error("NULL pointer error\n");
1066                  return NULL;                  return NULL;
1067          }          }
1068    
# Line 863  int section_list_calculate_page(SECTION_ Line 1154  int section_list_calculate_page(SECTION_
1154    
1155          if (p_section == NULL)          if (p_section == NULL)
1156          {          {
1157                  log_error("section_list_calculate_page() NULL pointer error\n");                  log_error("NULL pointer error\n");
1158                  return -1;                  return -1;
1159          }          }
1160    
# Line 957  int section_list_calculate_page(SECTION_ Line 1248  int section_list_calculate_page(SECTION_
1248          return 0;          return 0;
1249  }  }
1250    
1251    int32_t article_block_last_aid(void)
1252    {
1253            ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1];
1254            int32_t last_aid = p_block->articles[p_block->article_count - 1].aid;
1255    
1256            return last_aid;
1257    }
1258    
1259    int article_block_article_count(void)
1260    {
1261            int ret;
1262    
1263            if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0)
1264            {
1265                    return -1;
1266            }
1267    
1268            ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK +
1269                      p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count;
1270    
1271            return ret;
1272    }
1273    
1274  int article_count_of_topic(int32_t aid)  int article_count_of_topic(int32_t aid)
1275  {  {
1276          ARTICLE *p_article;          ARTICLE *p_article;
# Line 997  int section_list_move_topic(SECTION_LIST Line 1311  int section_list_move_topic(SECTION_LIST
1311          int32_t first_inserted_aid_dest;          int32_t first_inserted_aid_dest;
1312          int move_counter;          int move_counter;
1313    
1314          if (p_section_dest == NULL)          if (p_section_src == NULL || p_section_dest == NULL)
1315          {          {
1316                  log_error("section_list_move_topic() NULL pointer error\n");                  log_error("NULL pointer error\n");
1317                    return -1;
1318            }
1319    
1320            if (p_section_src->sid == p_section_dest->sid)
1321            {
1322                    log_error("section_list_move_topic() src and dest section are the same\n");
1323                  return -1;                  return -1;
1324          }          }
1325    
1326          if ((p_article = article_block_find_by_aid(aid)) == NULL)          if ((p_article = article_block_find_by_aid(aid)) == NULL)
1327          {          {
1328                  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);
1329                  return -2;                  return -2;
1330          }          }
1331    
# Line 1027  int section_list_move_topic(SECTION_LIST Line 1347  int section_list_move_topic(SECTION_LIST
1347          move_article_count = article_count_of_topic(aid);          move_article_count = article_count_of_topic(aid);
1348          if (move_article_count <= 0)          if (move_article_count <= 0)
1349          {          {
1350                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1351                  return -2;                  return -2;
1352          }          }
1353    
# Line 1046  int section_list_move_topic(SECTION_LIST Line 1366  int section_list_move_topic(SECTION_LIST
1366          {          {
1367                  if (p_section_src->sid != p_article->sid)                  if (p_section_src->sid != p_article->sid)
1368                  {                  {
1369                          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",
1370                                            p_section_src->sid, p_article->aid, p_article->sid);                                            p_section_src->sid, p_article->aid, p_article->sid);
1371                          return -2;                          p_article = p_article->p_topic_next;
1372                            continue;
1373                  }                  }
1374    
1375                  // 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 1395  int section_list_move_topic(SECTION_LIST
1395    
1396                  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)
1397                  {                  {
1398                          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);
1399                          return -4;                          p_article = p_article->p_topic_next;
1400                            continue;
1401                  }                  }
1402    
1403                  // 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 1475  int section_list_move_topic(SECTION_LIST
1475    
1476          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)
1477          {          {
1478                  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",
1479                                    p_section_dest->article_count - dest_article_count_old, move_article_count);                                    p_section_dest->article_count - dest_article_count_old, move_article_count);
1480          }          }
1481    
# Line 1193  int get_section_index(SECTION_LIST *p_se Line 1515  int get_section_index(SECTION_LIST *p_se
1515          }          }
1516          else          else
1517          {          {
1518                  index = (int)(p_section - p_section_list_pool);                  index = (int)(p_section - p_section_list_pool->sections);
1519                  if (index < 0 || index >= BBS_max_section)                  if (index < 0 || index >= BBS_max_section)
1520                  {                  {
1521                          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 1207  int get_section_index(SECTION_LIST *p_se Line 1529  int get_section_index(SECTION_LIST *p_se
1529  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)
1530  {  {
1531          int index;          int index;
1532          struct sembuf sops[2];          struct sembuf sops[4];
1533          struct timespec timeout;          struct timespec timeout;
1534          int ret;          int ret;
1535    
# Line 1217  int section_list_try_rd_lock(SECTION_LIS Line 1539  int section_list_try_rd_lock(SECTION_LIS
1539                  return -2;                  return -2;
1540          }          }
1541    
1542          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
1543          sops[0].sem_op = 0;                                                      // check if unlocked          sops[0].sem_op = 0;                                                                // wait until unlocked
1544          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
1545    
1546          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
1547          sops[1].sem_op = 1;                                              // lock          sops[1].sem_op = 1;                                                        // lock
1548          sops[1].sem_flg = SEM_UNDO;                              // undo on terminate          sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1549    
1550            // Read lock on any specific section will also acquire single read lock on "all section"
1551            // so that write lock on all section only need to acquire single write on on "all section"
1552            // rather than to acquire multiple write locks on all the available sections.
1553            if (index != BBS_max_section)
1554            {
1555                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1556                    sops[2].sem_op = 0;                                                // wait until unlocked
1557                    sops[2].sem_flg = 0;
1558    
1559                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1560                    sops[3].sem_op = 1;                                        // lock
1561                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1562            }
1563    
1564          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1565          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1566    
1567          ret = semtimedop(section_list_pool_semid, sops, 2, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1568            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1569            {
1570                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1571            }
1572    
1573          return ret;          return ret;
1574  }  }
# Line 1246  int section_list_try_rw_lock(SECTION_LIS Line 1586  int section_list_try_rw_lock(SECTION_LIS
1586                  return -2;                  return -2;
1587          }          }
1588    
1589          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
1590          sops[0].sem_op = 0;                                                      // check if unlocked          sops[0].sem_op = 0;                                                                // wait until unlocked
1591          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
1592    
1593          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
1594          sops[1].sem_op = 1;                                                      // lock          sops[1].sem_op = 1;                                                                // lock
1595          sops[1].sem_flg = SEM_UNDO;                                      // undo on terminate          sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1596    
1597          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
1598          sops[1].sem_op = 0;                                              // wait until unlocked          sops[2].sem_op = 0;                                                        // wait until unlocked
1599          sops[1].sem_flg = 0;          sops[2].sem_flg = 0;
1600    
1601          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1602          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1603    
1604          ret = semtimedop(section_list_pool_semid, sops, 3, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1605            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1606            {
1607                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1608            }
1609    
1610          return ret;          return ret;
1611  }  }
# Line 1269  int section_list_try_rw_lock(SECTION_LIS Line 1613  int section_list_try_rw_lock(SECTION_LIS
1613  int section_list_rd_unlock(SECTION_LIST *p_section)  int section_list_rd_unlock(SECTION_LIST *p_section)
1614  {  {
1615          int index;          int index;
1616          struct sembuf sops[1];          struct sembuf sops[2];
1617          int ret;          int ret;
1618    
1619          index = get_section_index(p_section);          index = get_section_index(p_section);
# Line 1278  int section_list_rd_unlock(SECTION_LIST Line 1622  int section_list_rd_unlock(SECTION_LIST
1622                  return -2;                  return -2;
1623          }          }
1624    
1625          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
1626          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                               // unlock
1627          sops[0].sem_flg = IPC_NOWAIT;                    // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1628    
1629            if (index != BBS_max_section)
1630            {
1631                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1632                    sops[1].sem_op = -1;                                     // unlock
1633                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1634            }
1635    
1636          ret = semop(section_list_pool_semid, sops, 1);          ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1637            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1638            {
1639                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1640            }
1641    
1642          return ret;          return ret;
1643  }  }
# Line 1299  int section_list_rw_unlock(SECTION_LIST Line 1654  int section_list_rw_unlock(SECTION_LIST
1654                  return -2;                  return -2;
1655          }          }
1656    
1657          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
1658          sops[0].sem_op = -1;                                             // unlock          sops[0].sem_op = -1;                                                       // unlock
1659          sops[0].sem_flg = IPC_NOWAIT;                            // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1660    
1661            ret = semop(p_section_list_pool->semid, sops, 1);
1662            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1663            {
1664                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1665            }
1666    
1667            return ret;
1668    }
1669    
1670    int section_list_rd_lock(SECTION_LIST *p_section)
1671    {
1672            int timer = 0;
1673            int sid = (p_section == NULL ? 0 : p_section->sid);
1674            int ret = -1;
1675    
1676            while (!SYS_server_exit)
1677            {
1678                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1679                    if (ret == 0) // success
1680                    {
1681                            break;
1682                    }
1683                    else if (errno == EAGAIN || errno == EINTR) // retry
1684                    {
1685                            timer++;
1686                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1687                            {
1688                                    log_error("section_list_try_rd_lock() tried %d times on section %d\n", sid, timer);
1689                            }
1690                    }
1691                    else // failed
1692                    {
1693                            log_error("section_list_try_rd_lock() failed on section %d\n", sid);
1694                            break;
1695                    }
1696            }
1697    
1698            return ret;
1699    }
1700    
1701    int section_list_rw_lock(SECTION_LIST *p_section)
1702    {
1703            int timer = 0;
1704            int sid = (p_section == NULL ? 0 : p_section->sid);
1705            int ret = -1;
1706    
1707          ret = semop(section_list_pool_semid, sops, 1);          while (!SYS_server_exit)
1708            {
1709                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1710                    if (ret == 0) // success
1711                    {
1712                            break;
1713                    }
1714                    else if (errno == EAGAIN || errno == EINTR) // retry
1715                    {
1716                            timer++;
1717                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1718                            {
1719                                    log_error("section_list_try_rw_lock() tried %d times on section %d\n", sid, timer);
1720                            }
1721                    }
1722                    else // failed
1723                    {
1724                            log_error("section_list_try_rw_lock() failed on section %d\n", sid);
1725                            break;
1726                    }
1727            }
1728    
1729          return ret;          return ret;
1730  }  }


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

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