/[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.15 by sysadm, Sat May 24 03:41:34 2025 UTC Revision 1.24 by sysadm, Mon May 26 04:47:45 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _GNU_SOURCE
18    
19  #include "section_list.h"  #include "section_list.h"
20  #include "log.h"  #include "log.h"
21  #include "trie_dict.h"  #include "trie_dict.h"
# Line 24  Line 26 
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <errno.h>  #include <errno.h>
28  #include <sys/param.h>  #include <sys/param.h>
29    #include <sys/sem.h>
30  #include <sys/shm.h>  #include <sys/shm.h>
31  #include <sys/ipc.h>  #include <sys/ipc.h>
32    
33    #ifdef _SEM_SEMUN_UNDEFINED
34    union semun
35    {
36            int val;                           /* Value for SETVAL */
37            struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
38            unsigned short *array; /* Array for GETALL, SETALL */
39            struct seminfo *__buf; /* Buffer for IPC_INFO
40                                                              (Linux-specific) */
41    };
42    #endif // #ifdef _SEM_SEMUN_UNDEFINED
43    
44    #define SECTION_TRY_LOCK_WAIT_TIME 1 // second
45    #define SECTION_TRY_LOCK_TIMES 10
46    
47  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate  #define ARTICLE_BLOCK_PER_SHM 400                 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate
48  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id)  #define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id)
49  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)  #define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT)
50    
51  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic  #define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic
# Line 38  Line 55 
55  struct article_block_t  struct article_block_t
56  {  {
57          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[ARTICLE_PER_BLOCK];
58          int32_t article_count;          int article_count;
59          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
60  };  };
61  typedef struct article_block_t ARTICLE_BLOCK;  typedef struct article_block_t ARTICLE_BLOCK;
62    
 struct article_block_shm_t  
 {  
         int shmid;  
         void *p_shm;  
 };  
 typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  
   
63  struct article_block_pool_t  struct article_block_pool_t
64  {  {
65          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          int shmid;
66            struct
67            {
68                    int shmid;
69                    void *p_shm;
70            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
71          int shm_count;          int shm_count;
72          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
73          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
74          int32_t block_count;          int block_count;
75  };  };
76  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
77    
78  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
79    
80  static int section_list_pool_shmid;  struct section_list_pool_t
81  static SECTION_LIST *p_section_list_pool = NULL;  {
82  static int section_list_count = 0;          int shmid;
83  static TRIE_NODE *p_trie_dict_section_by_name = NULL;          SECTION_LIST sections[BBS_max_section];
84  static TRIE_NODE *p_trie_dict_section_by_sid = NULL;          int section_count;
85            int semid;
86            TRIE_NODE *p_trie_dict_section_by_name;
87            TRIE_NODE *p_trie_dict_section_by_sid;
88    };
89    typedef struct section_list_pool_t SECTION_LIST_POOL;
90    
91    static SECTION_LIST_POOL *p_section_list_pool = NULL;
92    
93  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
94  {  {
# Line 92  int article_block_init(const char *filen Line 114  int article_block_init(const char *filen
114                  return -2;                  return -2;
115          }          }
116    
117          p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL));          // Allocate shared memory
118          if (p_article_block_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
119            key = ftok(filename, proj_id);
120            if (key == -1)
121          {          {
122                  log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n");                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
123                  return -2;                  return -3;
124          }          }
125    
126          // Allocate shared memory          size = sizeof(ARTICLE_BLOCK_POOL);
127            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
128            if (shmid == -1)
129            {
130                    log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
131                    return -3;
132            }
133            p_shm = shmat(shmid, NULL, 0);
134            if (p_shm == (void *)-1)
135            {
136                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
137                    return -3;
138            }
139    
140            p_article_block_pool = p_shm;
141            p_article_block_pool->shmid = shmid;
142    
143          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
144          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
145    
# Line 108  int article_block_init(const char *filen Line 148  int article_block_init(const char *filen
148                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
149                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
150    
151                  proj_id = getpid() + p_article_block_pool->shm_count;                  proj_id = p_article_block_pool->shm_count;
152                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
153                  if (key == -1)                  if (key == -1)
154                  {                  {
# Line 117  int article_block_init(const char *filen Line 157  int article_block_init(const char *filen
157                          return -3;                          return -3;
158                  }                  }
159    
160                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
161                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
162                  if (shmid == -1)                  if (shmid == -1)
163                  {                  {
# Line 161  int article_block_init(const char *filen Line 201  int article_block_init(const char *filen
201    
202  void article_block_cleanup(void)  void article_block_cleanup(void)
203  {  {
204          if (p_article_block_pool != NULL)          int shmid;
205    
206            if (p_article_block_pool == NULL)
207            {
208                    return;
209            }
210    
211            for (int i = 0; i < p_article_block_pool->shm_count; i++)
212          {          {
213                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
214                  {                  {
215                          if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)                          log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
216                          {                  }
                                 log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);  
                         }  
217    
218                          if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)                  if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1)
219                          {                  {
220                                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);                          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);
                         }  
221                  }                  }
222            }
223    
224            shmid = p_article_block_pool->shmid;
225    
226                  free(p_article_block_pool);          if (shmdt(p_article_block_pool) == -1)
227                  p_article_block_pool = NULL;          {
228                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
229          }          }
230    
231            if (shmctl(shmid, IPC_RMID, NULL) == -1)
232            {
233                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
234            }
235    
236            p_article_block_pool = NULL;
237  }  }
238    
239  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 315  ARTICLE *article_block_find_by_index(int Line 370  ARTICLE *article_block_find_by_index(int
370          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
371  }  }
372    
373  extern int section_list_pool_init(const char *filename)  extern int section_list_init(const char *filename)
374  {  {
375            int semid;
376          int shmid;          int shmid;
377          int proj_id;          int proj_id;
378          key_t key;          key_t key;
379          size_t size;          size_t size;
380          void *p_shm;          void *p_shm;
381            union semun arg;
382            int i;
383    
384          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)  
385          {          {
386                  log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);                  section_list_cleanup();
                 return -1;  
387          }          }
388    
389          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 343  extern int section_list_pool_init(const Line 394  extern int section_list_pool_init(const
394                  return -3;                  return -3;
395          }          }
396    
397          size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section;          // Allocate shared memory
398            size = sizeof(SECTION_LIST_POOL);
399          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
400          if (shmid == -1)          if (shmid == -1)
401          {          {
402                  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);
403                  return -3;                  return -3;
404          }          }
405          p_shm = shmat(shmid, NULL, 0);          p_shm = shmat(shmid, NULL, 0);
# Line 357  extern int section_list_pool_init(const Line 409  extern int section_list_pool_init(const
409                  return -3;                  return -3;
410          }          }
411    
         section_list_pool_shmid = shmid;  
412          p_section_list_pool = p_shm;          p_section_list_pool = p_shm;
413          section_list_count = 0;          p_section_list_pool->shmid = shmid;
414            p_section_list_pool->section_count = 0;
415    
416          p_trie_dict_section_by_name = trie_dict_create();          // Allocate semaphore as section locks
417          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
418            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
419            if (semid == -1)
420            {
421                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
422                    return -3;
423            }
424    
425            // Initialize sem value to 0
426            arg.val = 0;
427            for (i = 0; i < size; i++)
428            {
429                    if (semctl(semid, i, SETVAL, arg) == -1)
430                    {
431                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
432                            return -3;
433                    }
434            }
435    
436            p_section_list_pool->semid = semid;
437    
438            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
439            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
440          {          {
441                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
442                  return -2;                  return -2;
443          }          }
444    
445          p_trie_dict_section_by_sid = trie_dict_create();          p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
446          if (p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
447          {          {
448                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
449                  return -2;                  return -2;
# Line 397  SECTION_LIST *section_list_create(int32_ Line 471  SECTION_LIST *section_list_create(int32_
471          SECTION_LIST *p_section;          SECTION_LIST *p_section;
472          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
473    
474          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)
475          {          {
476                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized\n");
477                  return NULL;                  return NULL;
478          }          }
479    
480          if (section_list_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
481          {          {
482                  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);
483                  return NULL;                  return NULL;
484          }          }
485    
486          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
487    
488          p_section = p_section_list_pool + section_list_count;          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
489    
490          p_section->sid = sid;          p_section->sid = sid;
491    
# Line 424  SECTION_LIST *section_list_create(int32_ Line 498  SECTION_LIST *section_list_create(int32_
498          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));          strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1));
499          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';
500    
501          if (trie_dict_set(p_trie_dict_section_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)
502          {          {
503                  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);
504                  return NULL;                  return NULL;
505          }          }
506    
507          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)
508          {          {
509                  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]);  
510                  return NULL;                  return NULL;
511          }          }
512    
513          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
514    
515          section_list_count++;          p_section_list_pool->section_count++;
516    
517          return p_section;          return p_section;
518  }  }
# Line 457  void section_list_reset_articles(SECTION Line 530  void section_list_reset_articles(SECTION
530          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
531  }  }
532    
533  void section_list_pool_cleanup(void)  void section_list_cleanup(void)
534  {  {
535          if (p_trie_dict_section_by_name != NULL)          int shmid;
536    
537            if (p_section_list_pool == NULL)
538          {          {
539                  trie_dict_destroy(p_trie_dict_section_by_name);                  return;
                 p_trie_dict_section_by_name = NULL;  
540          }          }
541    
542          if (p_trie_dict_section_by_sid != NULL)          if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
543          {          {
544                  trie_dict_destroy(p_trie_dict_section_by_sid);                  trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
545                  p_trie_dict_section_by_sid = NULL;                  p_section_list_pool->p_trie_dict_section_by_name = NULL;
546            }
547    
548            if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
549            {
550                    trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
551                    p_section_list_pool->p_trie_dict_section_by_sid = NULL;
552          }          }
553    
554          if (p_section_list_pool != NULL)          if (p_section_list_pool != NULL)
555          {          {
556                    if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
557                    {
558                            log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
559                    }
560    
561                    shmid = p_section_list_pool->shmid;
562    
563                  if (shmdt(p_section_list_pool) == -1)                  if (shmdt(p_section_list_pool) == -1)
564                  {                  {
565                          log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno);                          log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
566                  }                  }
                 p_section_list_pool = NULL;  
567    
568                  if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1)                  if (shmctl(shmid, IPC_RMID, NULL) == -1)
569                  {                  {
570                          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno);                          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
571                  }                  }
         }  
572    
573          section_list_count = 0;                  p_section_list_pool = NULL;
574            }
575  }  }
576    
577  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
578  {  {
579          int64_t index;          int64_t index;
580            int ret;
581    
582          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL)          if (p_section_list_pool == NULL)
583          {          {
584                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
585                  return NULL;                  return NULL;
586          }          }
587    
588          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);
589            if (ret < 0)
590          {          {
591                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
592                  return NULL;                  return NULL;
593          }          }
594            else if (ret == 0)
595            {
596                    return NULL;
597            }
598    
599          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
600  }  }
601    
602  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
603  {  {
604          int64_t index;          int64_t index;
605            int ret;
606          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
607    
608          if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL)
609          {          {
610                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
611                  return NULL;                  return NULL;
# Line 520  SECTION_LIST *section_list_find_by_sid(i Line 613  SECTION_LIST *section_list_find_by_sid(i
613    
614          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
615    
616          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);
617            if (ret < 0)
618          {          {
619                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
620                  return NULL;                  return NULL;
621          }          }
622            else if (ret == 0)
623            {
624                    return NULL;
625            }
626    
627          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
628  }  }
629    
630  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 1007  int section_list_move_topic(SECTION_LIST Line 1105  int section_list_move_topic(SECTION_LIST
1105          move_article_count = article_count_of_topic(aid);          move_article_count = article_count_of_topic(aid);
1106          if (move_article_count <= 0)          if (move_article_count <= 0)
1107          {          {
1108                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1109                  return -2;                  return -2;
1110          }          }
1111    
# Line 1156  int section_list_move_topic(SECTION_LIST Line 1254  int section_list_move_topic(SECTION_LIST
1254    
1255          return move_article_count;          return move_article_count;
1256  }  }
1257    
1258    int get_section_index(SECTION_LIST *p_section)
1259    {
1260            int index;
1261    
1262            if (p_section_list_pool == NULL)
1263            {
1264                    log_error("get_section_index() error: uninitialized\n");
1265                    return -1;
1266            }
1267    
1268            if (p_section == NULL)
1269            {
1270                    index = BBS_max_section;
1271            }
1272            else
1273            {
1274                    index = (int)(p_section - p_section_list_pool->sections);
1275                    if (index < 0 || index >= BBS_max_section)
1276                    {
1277                            log_error("get_section_index(%d) error: index out of range\n", index);
1278                            return -2;
1279                    }
1280            }
1281    
1282            return index;
1283    }
1284    
1285    int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec)
1286    {
1287            int index;
1288            struct sembuf sops[4];
1289            struct timespec timeout;
1290            int ret;
1291    
1292            index = get_section_index(p_section);
1293            if (index < 0)
1294            {
1295                    return -2;
1296            }
1297    
1298            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1299            sops[0].sem_op = 0;                                                                // wait until unlocked
1300            sops[0].sem_flg = 0;
1301    
1302            sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index
1303            sops[1].sem_op = 1;                                                        // lock
1304            sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1305    
1306            // Read lock on any specific section will also acquire single read lock on "all section"
1307            // so that write lock on all section only need to acquire single write on on "all section"
1308            // rather than to acquire multiple write locks on all the available sections.
1309            if (index != BBS_max_section)
1310            {
1311                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1312                    sops[2].sem_op = 0;                                                // wait until unlocked
1313                    sops[2].sem_flg = 0;
1314    
1315                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1316                    sops[3].sem_op = 1;                                        // lock
1317                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1318            }
1319    
1320            timeout.tv_sec = wait_sec;
1321            timeout.tv_nsec = 0;
1322    
1323            ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1324            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1325            {
1326                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1327            }
1328    
1329            return ret;
1330    }
1331    
1332    int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec)
1333    {
1334            int index;
1335            struct sembuf sops[3];
1336            struct timespec timeout;
1337            int ret;
1338    
1339            index = get_section_index(p_section);
1340            if (index < 0)
1341            {
1342                    return -2;
1343            }
1344    
1345            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1346            sops[0].sem_op = 0;                                                                // wait until unlocked
1347            sops[0].sem_flg = 0;
1348    
1349            sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1350            sops[1].sem_op = 1;                                                                // lock
1351            sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1352    
1353            sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index
1354            sops[2].sem_op = 0;                                                        // wait until unlocked
1355            sops[2].sem_flg = 0;
1356    
1357            timeout.tv_sec = wait_sec;
1358            timeout.tv_nsec = 0;
1359    
1360            ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1361            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1362            {
1363                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1364            }
1365    
1366            return ret;
1367    }
1368    
1369    int section_list_rd_unlock(SECTION_LIST *p_section)
1370    {
1371            int index;
1372            struct sembuf sops[2];
1373            int ret;
1374    
1375            index = get_section_index(p_section);
1376            if (index < 0)
1377            {
1378                    return -2;
1379            }
1380    
1381            sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index
1382            sops[0].sem_op = -1;                                               // unlock
1383            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1384    
1385            if (index != BBS_max_section)
1386            {
1387                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1388                    sops[1].sem_op = -1;                                     // unlock
1389                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1390            }
1391    
1392            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1393            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1394            {
1395                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1396            }
1397    
1398            return ret;
1399    }
1400    
1401    int section_list_rw_unlock(SECTION_LIST *p_section)
1402    {
1403            int index;
1404            struct sembuf sops[1];
1405            int ret;
1406    
1407            index = get_section_index(p_section);
1408            if (index < 0)
1409            {
1410                    return -2;
1411            }
1412    
1413            sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index
1414            sops[0].sem_op = -1;                                                       // unlock
1415            sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1416    
1417            ret = semop(p_section_list_pool->semid, sops, 1);
1418            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1419            {
1420                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1421            }
1422    
1423            return ret;
1424    }
1425    
1426    int section_list_rd_lock(SECTION_LIST *p_section)
1427    {
1428            int timer = 0;
1429            int sid = (p_section == NULL ? 0 : p_section->sid);
1430            int ret = -1;
1431    
1432            while (!SYS_server_exit)
1433            {
1434                    ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1435                    if (ret == 0) // success
1436                    {
1437                            break;
1438                    }
1439                    else if (errno == EAGAIN || errno == EINTR) // retry
1440                    {
1441                            timer++;
1442                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1443                            {
1444                                    log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer);
1445                            }
1446                    }
1447                    else // failed
1448                    {
1449                            log_error("section_list_rd_lock() failed on section %d\n", sid);
1450                            break;
1451                    }
1452            }
1453    
1454            return ret;
1455    }
1456    
1457    int section_list_rw_lock(SECTION_LIST *p_section)
1458    {
1459            int timer = 0;
1460            int sid = (p_section == NULL ? 0 : p_section->sid);
1461            int ret = -1;
1462    
1463            while (!SYS_server_exit)
1464            {
1465                    ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME);
1466                    if (ret == 0) // success
1467                    {
1468                            break;
1469                    }
1470                    else if (errno == EAGAIN || errno == EINTR) // retry
1471                    {
1472                            timer++;
1473                            if (timer % SECTION_TRY_LOCK_TIMES == 0)
1474                            {
1475                                    log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer);
1476                            }
1477                    }
1478                    else // failed
1479                    {
1480                            log_error("acquire_section_rw_lock() failed on section %d\n", sid);
1481                            break;
1482                    }
1483            }
1484    
1485            return ret;
1486    }


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

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