/[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.22 by sysadm, Sun May 25 23:47:36 2025 UTC
# Line 30  Line 30 
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 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
45  #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)
46  #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)
47    
48  #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 41  Line 52 
52  struct article_block_t  struct article_block_t
53  {  {
54          ARTICLE articles[ARTICLE_PER_BLOCK];          ARTICLE articles[ARTICLE_PER_BLOCK];
55          int32_t article_count;          int article_count;
56          struct article_block_t *p_next_block;          struct article_block_t *p_next_block;
57  };  };
58  typedef struct article_block_t ARTICLE_BLOCK;  typedef struct article_block_t ARTICLE_BLOCK;
59    
 struct article_block_shm_t  
 {  
         int shmid;  
         void *p_shm;  
 };  
 typedef struct article_block_shm_t ARTICLE_BLOCK_SHM;  
   
60  struct article_block_pool_t  struct article_block_pool_t
61  {  {
62          ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];          int shmid;
63            struct
64            {
65                    int shmid;
66                    void *p_shm;
67            } shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT];
68          int shm_count;          int shm_count;
69          ARTICLE_BLOCK *p_block_free_list;          ARTICLE_BLOCK *p_block_free_list;
70          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];          ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL];
71          int32_t block_count;          int block_count;
72  };  };
73  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;  typedef struct article_block_pool_t ARTICLE_BLOCK_POOL;
74    
75  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;  static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL;
76    
77  static int section_list_pool_shmid;  struct section_list_pool_t
78  static int section_list_pool_semid;  {
79  static SECTION_LIST *p_section_list_pool = NULL;          int shmid;
80  static int section_list_count = 0;          SECTION_LIST sections[BBS_max_section];
81  static TRIE_NODE *p_trie_dict_section_by_name = NULL;          int section_count;
82  static TRIE_NODE *p_trie_dict_section_by_sid = NULL;          int semid;
83            TRIE_NODE *p_trie_dict_section_by_name;
84            TRIE_NODE *p_trie_dict_section_by_sid;
85    };
86    typedef struct section_list_pool_t SECTION_LIST_POOL;
87    
88    static SECTION_LIST_POOL *p_section_list_pool = NULL;
89    
90  int article_block_init(const char *filename, int block_count)  int article_block_init(const char *filename, int block_count)
91  {  {
# Line 96  int article_block_init(const char *filen Line 111  int article_block_init(const char *filen
111                  return -2;                  return -2;
112          }          }
113    
114          p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL));          // Allocate shared memory
115          if (p_article_block_pool == NULL)          proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm
116            key = ftok(filename, proj_id);
117            if (key == -1)
118          {          {
119                  log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n");                  log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno);
120                  return -2;                  return -3;
121          }          }
122    
123          // Allocate shared memory          size = sizeof(ARTICLE_BLOCK_POOL);
124            shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
125            if (shmid == -1)
126            {
127                    log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno);
128                    return -3;
129            }
130            p_shm = shmat(shmid, NULL, 0);
131            if (p_shm == (void *)-1)
132            {
133                    log_error("shmat(shmid = %d) error (%d)\n", shmid, errno);
134                    return -3;
135            }
136    
137            p_article_block_pool = p_shm;
138            p_article_block_pool->shmid = shmid;
139    
140          p_article_block_pool->shm_count = 0;          p_article_block_pool->shm_count = 0;
141          pp_block_next = &(p_article_block_pool->p_block_free_list);          pp_block_next = &(p_article_block_pool->p_block_free_list);
142    
# Line 112  int article_block_init(const char *filen Line 145  int article_block_init(const char *filen
145                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);                  block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM);
146                  block_count -= block_count_in_shm;                  block_count -= block_count_in_shm;
147    
148                  proj_id = getpid() + p_article_block_pool->shm_count;                  proj_id = p_article_block_pool->shm_count;
149                  key = ftok(filename, proj_id);                  key = ftok(filename, proj_id);
150                  if (key == -1)                  if (key == -1)
151                  {                  {
# Line 121  int article_block_init(const char *filen Line 154  int article_block_init(const char *filen
154                          return -3;                          return -3;
155                  }                  }
156    
157                  size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;                  size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm;
158                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);                  shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
159                  if (shmid == -1)                  if (shmid == -1)
160                  {                  {
# Line 165  int article_block_init(const char *filen Line 198  int article_block_init(const char *filen
198    
199  void article_block_cleanup(void)  void article_block_cleanup(void)
200  {  {
201          if (p_article_block_pool != NULL)          int shmid;
202    
203            if (p_article_block_pool == NULL)
204          {          {
205                  for (int i = 0; i < p_article_block_pool->shm_count; i++)                  return;
206            }
207    
208            for (int i = 0; i < p_article_block_pool->shm_count; i++)
209            {
210                    if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1)
211                  {                  {
212                          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);
213                          {                  }
                                 log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno);  
                         }  
214    
215                          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)
216                          {                  {
217                                  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);
                         }  
218                  }                  }
219            }
220    
221            shmid = p_article_block_pool->shmid;
222    
223                  free(p_article_block_pool);          if (shmdt(p_article_block_pool) == -1)
224                  p_article_block_pool = NULL;          {
225                    log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
226            }
227    
228            if (shmctl(shmid, IPC_RMID, NULL) == -1)
229            {
230                    log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
231          }          }
232    
233            p_article_block_pool = NULL;
234  }  }
235    
236  inline static ARTICLE_BLOCK *pop_free_article_block(void)  inline static ARTICLE_BLOCK *pop_free_article_block(void)
# Line 319  ARTICLE *article_block_find_by_index(int Line 367  ARTICLE *article_block_find_by_index(int
367          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
368  }  }
369    
370  extern int section_list_pool_init(const char *filename)  extern int section_list_init(const char *filename)
371  {  {
372          int semid;          int semid;
373          int shmid;          int shmid;
# Line 327  extern int section_list_pool_init(const Line 375  extern int section_list_pool_init(const
375          key_t key;          key_t key;
376          size_t size;          size_t size;
377          void *p_shm;          void *p_shm;
378            union semun arg;
379            int i;
380    
381          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)  
382          {          {
383                  log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section);                  section_list_cleanup();
                 return -1;  
384          }          }
385    
386          proj_id = (int)(time(NULL) % getpid());          proj_id = (int)(time(NULL) % getpid());
# Line 348  extern int section_list_pool_init(const Line 391  extern int section_list_pool_init(const
391                  return -3;                  return -3;
392          }          }
393    
394          size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections          // Allocate shared memory
395          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;  
396          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
397          if (shmid == -1)          if (shmid == -1)
398          {          {
399                  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);
400                  return -3;                  return -3;
401          }          }
402          p_shm = shmat(shmid, NULL, 0);          p_shm = shmat(shmid, NULL, 0);
# Line 372  extern int section_list_pool_init(const Line 406  extern int section_list_pool_init(const
406                  return -3;                  return -3;
407          }          }
408    
         section_list_pool_shmid = shmid;  
409          p_section_list_pool = p_shm;          p_section_list_pool = p_shm;
410          section_list_count = 0;          p_section_list_pool->shmid = shmid;
411            p_section_list_pool->section_count = 0;
412    
413            // Allocate semaphore as section locks
414            size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections
415            semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
416            if (semid == -1)
417            {
418                    log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno);
419                    return -3;
420            }
421    
422          p_trie_dict_section_by_name = trie_dict_create();          // Initialize sem value to 0
423          if (p_trie_dict_section_by_name == NULL)          arg.val = 0;
424            for (i = 0; i < size; i++)
425            {
426                    if (semctl(semid, i, SETVAL, arg) == -1)
427                    {
428                            log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno);
429                            return -3;
430                    }
431            }
432    
433            p_section_list_pool->semid = semid;
434    
435            p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create();
436            if (p_section_list_pool->p_trie_dict_section_by_name == NULL)
437          {          {
438                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
439                  return -2;                  return -2;
440          }          }
441    
442          p_trie_dict_section_by_sid = trie_dict_create();          p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create();
443          if (p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool->p_trie_dict_section_by_sid == NULL)
444          {          {
445                  log_error("trie_dict_create() OOM\n", BBS_max_section);                  log_error("trie_dict_create() OOM\n", BBS_max_section);
446                  return -2;                  return -2;
# Line 412  SECTION_LIST *section_list_create(int32_ Line 468  SECTION_LIST *section_list_create(int32_
468          SECTION_LIST *p_section;          SECTION_LIST *p_section;
469          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
470    
471          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)
472          {          {
473                  log_error("session_list_pool not initialized\n");                  log_error("session_list_pool not initialized\n");
474                  return NULL;                  return NULL;
475          }          }
476    
477          if (section_list_count >= BBS_max_section)          if (p_section_list_pool->section_count >= BBS_max_section)
478          {          {
479                  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);
480                  return NULL;                  return NULL;
481          }          }
482    
483          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
484    
485          p_section = p_section_list_pool + section_list_count;          p_section = p_section_list_pool->sections + p_section_list_pool->section_count;
486    
487          p_section->sid = sid;          p_section->sid = sid;
488    
# Line 439  SECTION_LIST *section_list_create(int32_ Line 495  SECTION_LIST *section_list_create(int32_
495          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));
496          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';          p_section->master_name[sizeof(p_section->master_name - 1)] = '\0';
497    
498          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)
499          {          {
500                  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);
501                  return NULL;                  return NULL;
502          }          }
503    
504          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)
505          {          {
506                  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]);  
507                  return NULL;                  return NULL;
508          }          }
509    
510          section_list_reset_articles(p_section);          section_list_reset_articles(p_section);
511    
512          section_list_count++;          p_section_list_pool->section_count++;
513    
514          return p_section;          return p_section;
515  }  }
# Line 472  void section_list_reset_articles(SECTION Line 527  void section_list_reset_articles(SECTION
527          p_section->last_page_visible_article_count = 0;          p_section->last_page_visible_article_count = 0;
528  }  }
529    
530  void section_list_pool_cleanup(void)  void section_list_cleanup(void)
531  {  {
532          if (p_trie_dict_section_by_name != NULL)          int shmid;
533    
534            if (p_section_list_pool == NULL)
535            {
536                    return;
537            }
538    
539            if (p_section_list_pool->p_trie_dict_section_by_name != NULL)
540          {          {
541                  trie_dict_destroy(p_trie_dict_section_by_name);                  trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name);
542                  p_trie_dict_section_by_name = NULL;                  p_section_list_pool->p_trie_dict_section_by_name = NULL;
543          }          }
544    
545          if (p_trie_dict_section_by_sid != NULL)          if (p_section_list_pool->p_trie_dict_section_by_sid != NULL)
546          {          {
547                  trie_dict_destroy(p_trie_dict_section_by_sid);                  trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid);
548                  p_trie_dict_section_by_sid = NULL;                  p_section_list_pool->p_trie_dict_section_by_sid = NULL;
549          }          }
550    
551          if (p_section_list_pool != NULL)          if (p_section_list_pool != NULL)
552          {          {
553                  if (shmdt(p_section_list_pool) == -1)                  if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1)
554                  {                  {
555                          log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno);                          log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno);
556                  }                  }
                 p_section_list_pool = NULL;  
557    
558                  if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1)                  shmid = p_section_list_pool->shmid;
559    
560                    if (shmdt(p_section_list_pool) == -1)
561                  {                  {
562                          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno);                          log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);
563                  }                  }
564    
565                  if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1)                  if (shmctl(shmid, IPC_RMID, NULL) == -1)
566                  {                  {
567                          log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno);                          log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);
568                  }                  }
         }  
569    
570          section_list_count = 0;                  p_section_list_pool = NULL;
571            }
572  }  }
573    
574  SECTION_LIST *section_list_find_by_name(const char *sname)  SECTION_LIST *section_list_find_by_name(const char *sname)
575  {  {
576          int64_t index;          int64_t index;
577    
578          if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL)          if (p_section_list_pool == NULL)
579          {          {
580                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
581                  return NULL;                  return NULL;
582          }          }
583    
584          if (trie_dict_get(p_trie_dict_section_by_name, sname, &index) != 1)          if (trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index) != 1)
585          {          {
586                  log_error("trie_dict_get(section, %s) error\n", sname);                  log_error("trie_dict_get(section, %s) error\n", sname);
587                  return NULL;                  return NULL;
588          }          }
589    
590          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
591  }  }
592    
593  SECTION_LIST *section_list_find_by_sid(int32_t sid)  SECTION_LIST *section_list_find_by_sid(int32_t sid)
# Line 532  SECTION_LIST *section_list_find_by_sid(i Line 595  SECTION_LIST *section_list_find_by_sid(i
595          int64_t index;          int64_t index;
596          char sid_str[SID_STR_LEN];          char sid_str[SID_STR_LEN];
597    
598          if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL)          if (p_section_list_pool == NULL)
599          {          {
600                  log_error("section_list not initialized\n");                  log_error("section_list not initialized\n");
601                  return NULL;                  return NULL;
# Line 540  SECTION_LIST *section_list_find_by_sid(i Line 603  SECTION_LIST *section_list_find_by_sid(i
603    
604          sid_to_str(sid, sid_str);          sid_to_str(sid, sid_str);
605    
606          if (trie_dict_get(p_trie_dict_section_by_sid, sid_str, &index) != 1)          if (trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index) != 1)
607          {          {
608                  log_error("trie_dict_get(section, %d) error\n", sid);                  log_error("trie_dict_get(section, %d) error\n", sid);
609                  return NULL;                  return NULL;
610          }          }
611    
612          return (p_section_list_pool + index);          return (p_section_list_pool->sections + index);
613  }  }
614    
615  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 1027  int section_list_move_topic(SECTION_LIST Line 1090  int section_list_move_topic(SECTION_LIST
1090          move_article_count = article_count_of_topic(aid);          move_article_count = article_count_of_topic(aid);
1091          if (move_article_count <= 0)          if (move_article_count <= 0)
1092          {          {
1093                  log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);                  log_error("article_count_of_topic(aid = %d) <= 0\n", aid);
1094                  return -2;                  return -2;
1095          }          }
1096    
# Line 1193  int get_section_index(SECTION_LIST *p_se Line 1256  int get_section_index(SECTION_LIST *p_se
1256          }          }
1257          else          else
1258          {          {
1259                  index = (int)(p_section - p_section_list_pool);                  index = (int)(p_section - p_section_list_pool->sections);
1260                  if (index < 0 || index >= BBS_max_section)                  if (index < 0 || index >= BBS_max_section)
1261                  {                  {
1262                          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 1270  int get_section_index(SECTION_LIST *p_se
1270  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)
1271  {  {
1272          int index;          int index;
1273          struct sembuf sops[2];          struct sembuf sops[4];
1274          struct timespec timeout;          struct timespec timeout;
1275          int ret;          int ret;
1276    
# Line 1217  int section_list_try_rd_lock(SECTION_LIS Line 1280  int section_list_try_rd_lock(SECTION_LIS
1280                  return -2;                  return -2;
1281          }          }
1282    
1283          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
1284          sops[0].sem_op = 0;                                                      // check if unlocked          sops[0].sem_op = 0;                                                                // wait until unlocked
1285          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
1286    
1287          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
1288          sops[1].sem_op = 1;                                              // lock          sops[1].sem_op = 1;                                                        // lock
1289          sops[1].sem_flg = SEM_UNDO;                              // undo on terminate          sops[1].sem_flg = SEM_UNDO;                                        // undo on terminate
1290    
1291            // Read lock on any specific section will also acquire single read lock on "all section"
1292            // so that write lock on all section only need to acquire single write on on "all section"
1293            // rather than to acquire multiple write locks on all the available sections.
1294            if (index != BBS_max_section)
1295            {
1296                    sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section
1297                    sops[2].sem_op = 0;                                                // wait until unlocked
1298                    sops[2].sem_flg = 0;
1299    
1300                    sops[3].sem_num = BBS_max_section * 2; // r_sem of all section
1301                    sops[3].sem_op = 1;                                        // lock
1302                    sops[3].sem_flg = SEM_UNDO;                        // undo on terminate
1303            }
1304    
1305          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1306          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1307    
1308          ret = semtimedop(section_list_pool_semid, sops, 2, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout);
1309            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1310            {
1311                    log_error("semtimedop(index = %d, lock read) error %d\n", index, errno);
1312            }
1313    
1314          return ret;          return ret;
1315  }  }
# Line 1246  int section_list_try_rw_lock(SECTION_LIS Line 1327  int section_list_try_rw_lock(SECTION_LIS
1327                  return -2;                  return -2;
1328          }          }
1329    
1330          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
1331          sops[0].sem_op = 0;                                                      // check if unlocked          sops[0].sem_op = 0;                                                                // wait until unlocked
1332          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
1333    
1334          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
1335          sops[1].sem_op = 1;                                                      // lock          sops[1].sem_op = 1;                                                                // lock
1336          sops[1].sem_flg = SEM_UNDO;                                      // undo on terminate          sops[1].sem_flg = SEM_UNDO;                                                // undo on terminate
1337    
1338          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
1339          sops[1].sem_op = 0;                                              // wait until unlocked          sops[2].sem_op = 0;                                                        // wait until unlocked
1340          sops[1].sem_flg = 0;          sops[2].sem_flg = 0;
1341    
1342          timeout.tv_sec = wait_sec;          timeout.tv_sec = wait_sec;
1343          timeout.tv_nsec = 0;          timeout.tv_nsec = 0;
1344    
1345          ret = semtimedop(section_list_pool_semid, sops, 3, &timeout);          ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout);
1346            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1347            {
1348                    log_error("semtimedop(index = %d, lock write) error %d\n", index, errno);
1349            }
1350    
1351          return ret;          return ret;
1352  }  }
# Line 1269  int section_list_try_rw_lock(SECTION_LIS Line 1354  int section_list_try_rw_lock(SECTION_LIS
1354  int section_list_rd_unlock(SECTION_LIST *p_section)  int section_list_rd_unlock(SECTION_LIST *p_section)
1355  {  {
1356          int index;          int index;
1357          struct sembuf sops[1];          struct sembuf sops[2];
1358          int ret;          int ret;
1359    
1360          index = get_section_index(p_section);          index = get_section_index(p_section);
# Line 1278  int section_list_rd_unlock(SECTION_LIST Line 1363  int section_list_rd_unlock(SECTION_LIST
1363                  return -2;                  return -2;
1364          }          }
1365    
1366          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
1367          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                               // unlock
1368          sops[0].sem_flg = IPC_NOWAIT;                    // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;           // no wait
1369    
1370          ret = semop(section_list_pool_semid, sops, 1);          if (index != BBS_max_section)
1371            {
1372                    sops[1].sem_num = BBS_max_section * 2;   // r_sem of all section
1373                    sops[1].sem_op = -1;                                     // unlock
1374                    sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
1375            }
1376    
1377            ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2));
1378            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1379            {
1380                    log_error("semop(index = %d, unlock read) error %d\n", index, errno);
1381            }
1382    
1383          return ret;          return ret;
1384  }  }
# Line 1299  int section_list_rw_unlock(SECTION_LIST Line 1395  int section_list_rw_unlock(SECTION_LIST
1395                  return -2;                  return -2;
1396          }          }
1397    
1398          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
1399          sops[0].sem_op = -1;                                             // unlock          sops[0].sem_op = -1;                                                       // unlock
1400          sops[0].sem_flg = IPC_NOWAIT;                            // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO;                   // no wait
1401    
1402          ret = semop(section_list_pool_semid, sops, 1);          ret = semop(p_section_list_pool->semid, sops, 1);
1403            if (ret == -1 && errno != EAGAIN && errno != EINTR)
1404            {
1405                    log_error("semop(index = %d, unlock write) error %d\n", index, errno);
1406            }
1407    
1408          return ret;          return ret;
1409  }  }


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

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