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

Diff of /lbbs/src/user_list.c

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

Revision 1.35 by sysadm, Mon Nov 17 12:16:48 2025 UTC Revision 1.43 by sysadm, Thu Nov 20 10:20:51 2025 UTC
# Line 17  Line 17 
17  #include "user_list.h"  #include "user_list.h"
18  #include "user_stat.h"  #include "user_stat.h"
19  #include <errno.h>  #include <errno.h>
20    #include <fcntl.h>
21  #include <stdlib.h>  #include <stdlib.h>
22  #include <string.h>  #include <string.h>
23  #include <time.h>  #include <time.h>
 #include <sys/ipc.h>  
24  #include <sys/mman.h>  #include <sys/mman.h>
25  #include <sys/param.h>  #include <sys/param.h>
26    #include <sys/stat.h>
27    
28    #ifdef HAVE_SYSTEM_V
29  #include <sys/sem.h>  #include <sys/sem.h>
 #include <sys/shm.h>  
30    
31  #if defined(_SEM_SEMUN_UNDEFINED) || defined(__MSYS__) || defined(__MINGW32__)  #ifdef _SEM_SEMUN_UNDEFINED
32  union semun  union semun
33  {  {
34          int val;                           /* Value for SETVAL */          int val;                           /* Value for SETVAL */
35          struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */          struct semid_ds *buf;  /* Buffer for IPC_STAT, IPC_SET */
36          unsigned short *array; /* Array for GETALL, SETALL */          unsigned short *array; /* Array for GETALL, SETALL */
37          struct seminfo *__buf; /* Buffer for IPC_INFO          struct seminfo *__buf; /* Buffer for IPC_INFO
38                                                            (Linux-specific) */                                                          (Linux-specific) */
39  };  };
40  #endif // #if defined(_SEM_SEMUN_UNDEFINED)  #endif // #ifdef _SEM_SEMUN_UNDEFINED
41    
42    #else
43    #include <semaphore.h>
44    #endif
45    
46  enum _user_list_constant_t  enum _user_list_constant_t
47  {  {
# Line 45  enum _user_list_constant_t Line 51  enum _user_list_constant_t
51    
52  struct user_list_pool_t  struct user_list_pool_t
53  {  {
54          int shmid;          size_t shm_size;
55    #ifndef HAVE_SYSTEM_V
56            sem_t sem;
57            uint16_t read_lock_count;
58            uint16_t write_lock_count;
59    #else
60          int semid;          int semid;
61    #endif
62          USER_LIST user_list[2];          USER_LIST user_list[2];
63          USER_LIST *p_current;          int user_list_index_current;
64          USER_LIST *p_new;          int user_list_index_new;
65          USER_ONLINE_LIST user_online_list[2];          USER_ONLINE_LIST user_online_list[2];
66          USER_ONLINE_LIST *p_online_current;          int user_online_list_index_current;
67          USER_ONLINE_LIST *p_online_new;          int user_online_list_index_new;
68          USER_STAT_MAP user_stat_map;          USER_STAT_MAP user_stat_map;
69          int user_login_count;          int user_login_count;
70  };  };
71  typedef struct user_list_pool_t USER_LIST_POOL;  typedef struct user_list_pool_t USER_LIST_POOL;
72    
73    static char user_list_shm_name[FILE_NAME_LEN];
74  static USER_LIST_POOL *p_user_list_pool = NULL;  static USER_LIST_POOL *p_user_list_pool = NULL;
75  static TRIE_NODE *p_trie_action_dict = NULL;  static TRIE_NODE *p_trie_action_dict = NULL;
76    
# Line 85  const USER_ACTION_MAP user_action_map[] Line 98  const USER_ACTION_MAP user_action_map[]
98    
99  const int user_action_map_size = sizeof(user_action_map) / sizeof(USER_ACTION_MAP);  const int user_action_map_size = sizeof(user_action_map) / sizeof(USER_ACTION_MAP);
100    
101  static int user_list_try_rd_lock(int semid, int wait_sec);  static int user_list_try_rd_lock(int wait_sec);
102  static int user_list_try_rw_lock(int semid, int wait_sec);  static int user_list_try_rw_lock(int wait_sec);
103  static int user_list_rd_unlock(int semid);  static int user_list_rd_unlock(void);
104  static int user_list_rw_unlock(int semid);  static int user_list_rw_unlock(void);
105  static int user_list_rd_lock(int semid);  static int user_list_rd_lock(void);
106  static int user_list_rw_lock(int semid);  static int user_list_rw_lock(void);
107    
108  static int user_list_load(MYSQL *db, USER_LIST *p_list);  static int user_list_load(MYSQL *db, USER_LIST *p_list);
109  static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_online_list);  static int user_online_list_load(MYSQL *db, USER_ONLINE_LIST *p_online_list);
# Line 388  int user_login_count_load(MYSQL *db) Line 401  int user_login_count_load(MYSQL *db)
401    
402  int user_list_pool_init(const char *filename)  int user_list_pool_init(const char *filename)
403  {  {
404          int shmid;          char filepath[FILE_PATH_LEN];
405          int semid;          int fd;
         int proj_id;  
         key_t key;  
406          size_t size;          size_t size;
407          void *p_shm;          void *p_shm;
408    #ifdef HAVE_SYSTEM_V
409            int proj_id;
410            key_t key;
411            int semid;
412          union semun arg;          union semun arg;
413    #endif
414          int i;          int i;
415    
416          if (p_user_list_pool != NULL || p_trie_action_dict != NULL)          if (p_user_list_pool != NULL || p_trie_action_dict != NULL)
# Line 419  int user_list_pool_init(const char *file Line 435  int user_list_pool_init(const char *file
435          }          }
436    
437          // Allocate shared memory          // Allocate shared memory
438          proj_id = (int)(time(NULL) % getpid());          size = sizeof(USER_LIST_POOL);
439          key = ftok(filename, proj_id);  
440          if (key == -1)          strncpy(filepath, filename, sizeof(filepath) - 1);
441            filepath[sizeof(filepath) - 1] = '\0';
442            snprintf(user_list_shm_name, sizeof(user_list_shm_name), "/USER_LIST_SHM_%s", basename(filepath));
443    
444            if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT)
445          {          {
446                  log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);                  log_error("shm_unlink(%s) error (%d)\n", user_list_shm_name, errno);
447                  return -2;                  return -2;
448          }          }
449    
450          size = sizeof(USER_LIST_POOL);          if ((fd = shm_open(user_list_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
         shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
         if (shmid == -1)  
451          {          {
452                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  log_error("shm_open(%s) error (%d)\n", user_list_shm_name, errno);
453                  return -3;                  return -2;
454          }          }
455          p_shm = shmat(shmid, NULL, 0);          if (ftruncate(fd, (off_t)size) == -1)
         if (p_shm == (void *)-1)  
456          {          {
457                  log_error("shmat(shmid=%d) error (%d)\n", shmid, errno);                  log_error("ftruncate(size=%d) error (%d)\n", size, errno);
458                  return -3;                  close(fd);
459                    return -2;
460            }
461    
462            p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
463            if (p_shm == MAP_FAILED)
464            {
465                    log_error("mmap() error (%d)\n", errno);
466                    close(fd);
467                    return -2;
468            }
469    
470            if (close(fd) < 0)
471            {
472                    log_error("close(fd) error (%d)\n", errno);
473                    return -1;
474          }          }
475    
476          p_user_list_pool = p_shm;          p_user_list_pool = p_shm;
477          p_user_list_pool->shmid = shmid;          p_user_list_pool->shm_size = size;
478    
479          // Allocate semaphore as user list pool lock          // Allocate semaphore as user list pool lock
480    #ifndef HAVE_SYSTEM_V
481            if (sem_init(&(p_user_list_pool->sem), 1, 1) == -1)
482            {
483                    log_error("sem_init() error (%d)\n", errno);
484                    return -3;
485            }
486    
487            p_user_list_pool->read_lock_count = 0;
488            p_user_list_pool->write_lock_count = 0;
489    #else
490            proj_id = (int)(time(NULL) % getpid());
491            key = ftok(filename, proj_id);
492            if (key == -1)
493            {
494                    log_error("ftok(%s %d) error (%d)\n", filename, proj_id, errno);
495                    return -2;
496            }
497    
498          size = 2; // r_sem and w_sem          size = 2; // r_sem and w_sem
499          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);          semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600);
500          if (semid == -1)          if (semid == -1)
# Line 465  int user_list_pool_init(const char *file Line 515  int user_list_pool_init(const char *file
515          }          }
516    
517          p_user_list_pool->semid = semid;          p_user_list_pool->semid = semid;
518    #endif
519    
520          // Set user counts to 0          // Set user counts to 0
521          p_user_list_pool->user_list[0].user_count = 0;          p_user_list_pool->user_list[0].user_count = 0;
522          p_user_list_pool->user_list[1].user_count = 0;          p_user_list_pool->user_list[1].user_count = 0;
523    
524          p_user_list_pool->p_current = &(p_user_list_pool->user_list[0]);          p_user_list_pool->user_list_index_current = 0;
525          p_user_list_pool->p_new = &(p_user_list_pool->user_list[1]);          p_user_list_pool->user_list_index_new = 1;
526    
527          p_user_list_pool->p_online_current = &(p_user_list_pool->user_online_list[0]);          p_user_list_pool->user_online_list_index_current = 0;
528          p_user_list_pool->p_online_new = &(p_user_list_pool->user_online_list[1]);          p_user_list_pool->user_online_list_index_new = 1;
529    
530          user_stat_map_init(&(p_user_list_pool->user_stat_map));          user_stat_map_init(&(p_user_list_pool->user_stat_map));
531    
# Line 483  int user_list_pool_init(const char *file Line 534  int user_list_pool_init(const char *file
534    
535  void user_list_pool_cleanup(void)  void user_list_pool_cleanup(void)
536  {  {
         int shmid;  
   
537          if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
538          {          {
539                  return;                  return;
540          }          }
541    
542          shmid = p_user_list_pool->shmid;  #ifdef HAVE_SYSTEM_V
   
543          if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)          if (semctl(p_user_list_pool->semid, 0, IPC_RMID) == -1)
544          {          {
545                  log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_user_list_pool->semid, errno);                  log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_user_list_pool->semid, errno);
546          }          }
547    #else
548          if (shmdt(p_user_list_pool) == -1)          if (sem_destroy(&(p_user_list_pool->sem)) == -1)
549          {          {
550                  log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno);                  log_error("sem_destroy() error (%d)\n", errno);
551          }          }
552    #endif
553    
554            detach_user_list_pool_shm();
555    
556          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)          if (shm_unlink(user_list_shm_name) == -1 && errno != ENOENT)
557          {          {
558                  log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shm_unlink(%s) error (%d)\n", user_list_shm_name, errno);
559          }          }
560    
561          p_user_list_pool = NULL;          user_list_shm_name[0] = '\0';
562    
563          if (p_trie_action_dict != NULL)          if (p_trie_action_dict != NULL)
564          {          {
# Line 519  void user_list_pool_cleanup(void) Line 570  void user_list_pool_cleanup(void)
570    
571  int set_user_list_pool_shm_readonly(void)  int set_user_list_pool_shm_readonly(void)
572  {  {
573          int shmid;          if (p_user_list_pool != NULL && mprotect(p_user_list_pool, p_user_list_pool->shm_size, PROT_READ) < 0)
         void *p_shm;  
   
         if (p_user_list_pool == NULL)  
574          {          {
575                  log_error("p_user_list_pool not initialized\n");                  log_error("mprotect() error (%d)\n", errno);
576                  return -1;                  return -1;
577          }          }
578    
         shmid = p_user_list_pool->shmid;  
   
         // Remap shared memory in read-only mode  
 #if defined(__MSYS__) || defined(__MINGW32__)  
         if (shmdt(p_user_list_pool) == -1)  
         {  
                 log_error("shmdt(user_list_pool) error (%d)\n", errno);  
                 return -1;  
         }  
         p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY);  
 #else  
         p_shm = shmat(shmid, p_user_list_pool, SHM_RDONLY | SHM_REMAP);  
 #endif  
         if (p_shm == (void *)-1)  
         {  
                 log_error("shmat(user_list_pool shmid = %d) error (%d)\n", shmid, errno);  
                 return -3;  
         }  
   
         p_user_list_pool = p_shm;  
   
579          return 0;          return 0;
580  }  }
581    
582  int detach_user_list_pool_shm(void)  int detach_user_list_pool_shm(void)
583  {  {
584          if (p_user_list_pool != NULL && shmdt(p_user_list_pool) == -1)          if (p_user_list_pool != NULL && munmap(p_user_list_pool, p_user_list_pool->shm_size) < 0)
585          {          {
586                  log_error("shmdt(user_list_pool) error (%d)\n", errno);                  log_error("munmap() error (%d)\n", errno);
587                  return -1;                  return -1;
588          }          }
589    
# Line 568  int detach_user_list_pool_shm(void) Line 595  int detach_user_list_pool_shm(void)
595  int user_list_pool_reload(int online_user)  int user_list_pool_reload(int online_user)
596  {  {
597          MYSQL *db = NULL;          MYSQL *db = NULL;
598          USER_LIST *p_tmp;          int tmp;
         USER_ONLINE_LIST *p_online_tmp;  
599          int ret = 0;          int ret = 0;
600    
601          if (p_user_list_pool == NULL)          if (p_user_list_pool == NULL)
# Line 587  int user_list_pool_reload(int online_use Line 613  int user_list_pool_reload(int online_use
613    
614          if (online_user)          if (online_user)
615          {          {
616                  if (user_online_list_load(db, p_user_list_pool->p_online_new) < 0)                  if (user_online_list_load(db, &(p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_new])) < 0)
617                  {                  {
618                          log_error("user_online_list_load() error\n");                          log_error("user_online_list_load() error\n");
619                          ret = -2;                          ret = -2;
# Line 603  int user_list_pool_reload(int online_use Line 629  int user_list_pool_reload(int online_use
629          }          }
630          else          else
631          {          {
632                  if (user_list_load(db, p_user_list_pool->p_new) < 0)                  if (user_list_load(db, &(p_user_list_pool->user_list[p_user_list_pool->user_list_index_new])) < 0)
633                  {                  {
634                          log_error("user_list_load() error\n");                          log_error("user_list_load() error\n");
635                          ret = -2;                          ret = -2;
# Line 614  int user_list_pool_reload(int online_use Line 640  int user_list_pool_reload(int online_use
640          mysql_close(db);          mysql_close(db);
641          db = NULL;          db = NULL;
642    
643          if (user_list_rw_lock(p_user_list_pool->semid) < 0)          if (user_list_rw_lock() < 0)
644          {          {
645                  log_error("user_list_rw_lock() error\n");                  log_error("user_list_rw_lock() error\n");
646                  ret = -3;                  ret = -3;
# Line 624  int user_list_pool_reload(int online_use Line 650  int user_list_pool_reload(int online_use
650          if (online_user)          if (online_user)
651          {          {
652                  // Swap p_online_current and p_online_new                  // Swap p_online_current and p_online_new
653                  p_online_tmp = p_user_list_pool->p_online_current;                  tmp = p_user_list_pool->user_online_list_index_current;
654                  p_user_list_pool->p_online_current = p_user_list_pool->p_online_new;                  p_user_list_pool->user_online_list_index_current = p_user_list_pool->user_online_list_index_new;
655                  p_user_list_pool->p_online_new = p_online_tmp;                  p_user_list_pool->user_online_list_index_new = tmp;
656          }          }
657          else          else
658          {          {
659                  // Swap p_current and p_new                  // Swap index_current and index_new
660                  p_tmp = p_user_list_pool->p_current;                  tmp = p_user_list_pool->user_list_index_current;
661                  p_user_list_pool->p_current = p_user_list_pool->p_new;                  p_user_list_pool->user_list_index_current = p_user_list_pool->user_list_index_new;
662                  p_user_list_pool->p_new = p_tmp;                  p_user_list_pool->user_list_index_new = tmp;
663          }          }
664    
665          if (user_list_rw_unlock(p_user_list_pool->semid) < 0)          if (user_list_rw_unlock() < 0)
666          {          {
667                  log_error("user_list_rw_unlock() error\n");                  log_error("user_list_rw_unlock() error\n");
668                  ret = -3;                  ret = -3;
# Line 649  cleanup: Line 675  cleanup:
675          return ret;          return ret;
676  }  }
677    
678  int user_list_try_rd_lock(int semid, int wait_sec)  int user_list_try_rd_lock(int wait_sec)
679  {  {
680    #ifdef HAVE_SYSTEM_V
681          struct sembuf sops[2];          struct sembuf sops[2];
 #if !defined(__MSYS__) && !defined(__MINGW32__)  
         struct timespec timeout;  
682  #endif  #endif
683          int ret;          struct timespec timeout;
684            int ret = 0;
685    
686            if (p_user_list_pool == NULL)
687            {
688                    log_error("p_user_list_pool not initialized\n");
689                    return -1;
690            }
691    
692            timeout.tv_sec = wait_sec;
693            timeout.tv_nsec = 0;
694    
695    #ifdef HAVE_SYSTEM_V
696          sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1; // w_sem
697          sops[0].sem_op = 0;      // wait until unlocked          sops[0].sem_op = 0;      // wait until unlocked
698          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
# Line 665  int user_list_try_rd_lock(int semid, int Line 701  int user_list_try_rd_lock(int semid, int
701          sops[1].sem_op = 1;                     // lock          sops[1].sem_op = 1;                     // lock
702          sops[1].sem_flg = SEM_UNDO; // undo on terminate          sops[1].sem_flg = SEM_UNDO; // undo on terminate
703    
704  #if defined(__MSYS__) || defined(__MINGW32__)          ret = semtimedop(p_user_list_pool->semid, sops, 2, &timeout);
         ret = semop(semid, sops, 2);  
 #else  
         timeout.tv_sec = wait_sec;  
         timeout.tv_nsec = 0;  
   
         ret = semtimedop(semid, sops, 2, &timeout);  
 #endif  
705          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
706          {          {
707                  log_error("semop(lock read) error %d\n", errno);                  log_error("semop(lock read) error %d\n", errno);
708          }          }
709    #else
710            if (sem_timedwait(&(p_user_list_pool->sem), &timeout) == -1)
711            {
712                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
713                    {
714                            log_error("sem_timedwait() error %d\n", errno);
715                    }
716                    return -1;
717            }
718    
719            if (p_user_list_pool->write_lock_count == 0)
720            {
721                    p_user_list_pool->read_lock_count++;
722            }
723            else
724            {
725                    errno = EAGAIN;
726                    ret = -1;
727            }
728    
729            if (sem_post(&(p_user_list_pool->sem)) == -1)
730            {
731                    log_error("sem_post() error %d\n", errno);
732                    return -1;
733            }
734    #endif
735    
736          return ret;          return ret;
737  }  }
738    
739  int user_list_try_rw_lock(int semid, int wait_sec)  int user_list_try_rw_lock(int wait_sec)
740  {  {
741    #ifdef HAVE_SYSTEM_V
742          struct sembuf sops[3];          struct sembuf sops[3];
 #if !defined(__MSYS__) && !defined(__MINGW32__)  
         struct timespec timeout;  
743  #endif  #endif
744          int ret;          struct timespec timeout;
745            int ret = 0;
746    
747            if (p_user_list_pool == NULL)
748            {
749                    log_error("p_user_list_pool not initialized\n");
750                    return -1;
751            }
752    
753            timeout.tv_sec = wait_sec;
754            timeout.tv_nsec = 0;
755    
756    #ifdef HAVE_SYSTEM_V
757          sops[0].sem_num = 1; // w_sem          sops[0].sem_num = 1; // w_sem
758          sops[0].sem_op = 0;      // wait until unlocked          sops[0].sem_op = 0;      // wait until unlocked
759          sops[0].sem_flg = 0;          sops[0].sem_flg = 0;
# Line 701  int user_list_try_rw_lock(int semid, int Line 766  int user_list_try_rw_lock(int semid, int
766          sops[2].sem_op = 0;      // wait until unlocked          sops[2].sem_op = 0;      // wait until unlocked
767          sops[2].sem_flg = 0;          sops[2].sem_flg = 0;
768    
769  #if defined(__MSYS__) || defined(__MINGW32__)          ret = semtimedop(p_user_list_pool->semid, sops, 3, &timeout);
         ret = semop(semid, sops, 3);  
 #else  
         timeout.tv_sec = wait_sec;  
         timeout.tv_nsec = 0;  
   
         ret = semtimedop(semid, sops, 3, &timeout);  
 #endif  
770          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
771          {          {
772                  log_error("semop(lock write) error %d\n", errno);                  log_error("semop(lock write) error %d\n", errno);
773          }          }
774    #else
775            if (sem_timedwait(&(p_user_list_pool->sem), &timeout) == -1)
776            {
777                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
778                    {
779                            log_error("sem_timedwait() error %d\n", errno);
780                    }
781                    return -1;
782            }
783    
784            if (p_user_list_pool->read_lock_count == 0 && p_user_list_pool->write_lock_count == 0)
785            {
786                    p_user_list_pool->write_lock_count++;
787            }
788            else
789            {
790                    errno = EAGAIN;
791                    ret = -1;
792            }
793    
794            if (sem_post(&(p_user_list_pool->sem)) == -1)
795            {
796                    log_error("sem_post() error %d\n", errno);
797                    return -1;
798            }
799    #endif
800    
801          return ret;          return ret;
802  }  }
803    
804  int user_list_rd_unlock(int semid)  int user_list_rd_unlock(void)
805  {  {
806    #ifdef HAVE_SYSTEM_V
807          struct sembuf sops[2];          struct sembuf sops[2];
808          int ret;  #endif
809            int ret = 0;
810    
811            if (p_user_list_pool == NULL)
812            {
813                    log_error("p_user_list_pool not initialized\n");
814                    return -1;
815            }
816    
817    #ifdef HAVE_SYSTEM_V
818          sops[0].sem_num = 0;                                     // r_sem          sops[0].sem_num = 0;                                     // r_sem
819          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                     // unlock
820          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
821    
822          ret = semop(semid, sops, 1);          ret = semop(p_user_list_pool->semid, sops, 1);
823          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
824          {          {
825                  log_error("semop(unlock read) error %d\n", errno);                  log_error("semop(unlock read) error %d\n", errno);
826          }          }
827    #else
828            if (sem_wait(&(p_user_list_pool->sem)) == -1)
829            {
830                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
831                    {
832                            log_error("sem_wait() error %d\n", errno);
833                    }
834                    return -1;
835            }
836    
837            if (p_user_list_pool->read_lock_count > 0)
838            {
839                    p_user_list_pool->read_lock_count--;
840            }
841            else
842            {
843                    log_error("read_lock_count already 0\n");
844            }
845    
846            if (sem_post(&(p_user_list_pool->sem)) == -1)
847            {
848                    log_error("sem_post() error %d\n", errno);
849                    return -1;
850            }
851    #endif
852    
853          return ret;          return ret;
854  }  }
855    
856  int user_list_rw_unlock(int semid)  int user_list_rw_unlock(void)
857  {  {
858    #ifdef HAVE_SYSTEM_V
859          struct sembuf sops[1];          struct sembuf sops[1];
860          int ret;  #endif
861            int ret = 0;
862    
863            if (p_user_list_pool == NULL)
864            {
865                    log_error("p_user_list_pool not initialized\n");
866                    return -1;
867            }
868    
869    #ifdef HAVE_SYSTEM_V
870          sops[0].sem_num = 1;                                     // w_sem          sops[0].sem_num = 1;                                     // w_sem
871          sops[0].sem_op = -1;                                     // unlock          sops[0].sem_op = -1;                                     // unlock
872          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait          sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait
873    
874          ret = semop(semid, sops, 1);          ret = semop(p_user_list_pool->semid, sops, 1);
875          if (ret == -1 && errno != EAGAIN && errno != EINTR)          if (ret == -1 && errno != EAGAIN && errno != EINTR)
876          {          {
877                  log_error("semop(unlock write) error %d\n", errno);                  log_error("semop(unlock write) error %d\n", errno);
878          }          }
879    #else
880            if (sem_wait(&(p_user_list_pool->sem)) == -1)
881            {
882                    if (errno != ETIMEDOUT && errno != EAGAIN && errno != EINTR)
883                    {
884                            log_error("sem_wait() error %d\n", errno);
885                    }
886                    return -1;
887            }
888    
889            if (p_user_list_pool->write_lock_count > 0)
890            {
891                    p_user_list_pool->write_lock_count--;
892            }
893            else
894            {
895                    log_error("write_lock_count already 0\n");
896            }
897    
898            if (sem_post(&(p_user_list_pool->sem)) == -1)
899            {
900                    log_error("sem_post() error %d\n", errno);
901                    return -1;
902            }
903    #endif
904    
905          return ret;          return ret;
906  }  }
907    
908  int user_list_rd_lock(int semid)  int user_list_rd_lock(void)
909  {  {
910          int timer = 0;          int timer = 0;
911          int ret = -1;          int ret = -1;
912    
913            if (p_user_list_pool == NULL)
914            {
915                    log_error("p_user_list_pool not initialized\n");
916                    return -1;
917            }
918    
919          while (!SYS_server_exit)          while (!SYS_server_exit)
920          {          {
921                  ret = user_list_try_rd_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME);                  ret = user_list_try_rd_lock(USER_LIST_TRY_LOCK_WAIT_TIME);
922                  if (ret == 0) // success                  if (ret == 0) // success
923                  {                  {
924                          break;                          break;
# Line 772  int user_list_rd_lock(int semid) Line 930  int user_list_rd_lock(int semid)
930                          {                          {
931                                  log_error("user_list_try_rd_lock() tried %d times\n", timer);                                  log_error("user_list_try_rd_lock() tried %d times\n", timer);
932                          }                          }
933                            usleep(100 * 1000); // 0.1 second
934                  }                  }
935                  else // failed                  else // failed
936                  {                  {
# Line 783  int user_list_rd_lock(int semid) Line 942  int user_list_rd_lock(int semid)
942          return ret;          return ret;
943  }  }
944    
945  int user_list_rw_lock(int semid)  int user_list_rw_lock(void)
946  {  {
947          int timer = 0;          int timer = 0;
948          int ret = -1;          int ret = -1;
949    
950            if (p_user_list_pool == NULL)
951            {
952                    log_error("p_user_list_pool not initialized\n");
953                    return -1;
954            }
955    
956          while (!SYS_server_exit)          while (!SYS_server_exit)
957          {          {
958                  ret = user_list_try_rw_lock(semid, USER_LIST_TRY_LOCK_WAIT_TIME);                  ret = user_list_try_rw_lock(USER_LIST_TRY_LOCK_WAIT_TIME);
959                  if (ret == 0) // success                  if (ret == 0) // success
960                  {                  {
961                          break;                          break;
# Line 802  int user_list_rw_lock(int semid) Line 967  int user_list_rw_lock(int semid)
967                          {                          {
968                                  log_error("user_list_try_rw_lock() tried %d times\n", timer);                                  log_error("user_list_try_rw_lock() tried %d times\n", timer);
969                          }                          }
970                            usleep(100 * 1000); // 0.1 second
971                  }                  }
972                  else // failed                  else // failed
973                  {                  {
# Line 827  int query_user_list(int page_id, USER_IN Line 993  int query_user_list(int page_id, USER_IN
993          *p_page_count = 0;          *p_page_count = 0;
994    
995          // acquire lock of user list          // acquire lock of user list
996          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
997          {          {
998                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
999                  return -2;                  return -2;
1000          }          }
1001    
1002          if (p_user_list_pool->p_current->user_count == 0)          if (p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count == 0)
1003          {          {
1004                  // empty list                  // empty list
1005                  ret = 0;                  ret = 0;
1006                  goto cleanup;                  goto cleanup;
1007          }          }
1008    
1009          *p_page_count = (p_user_list_pool->p_current->user_count + BBS_user_limit_per_page - 1) /          *p_page_count = (p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count + BBS_user_limit_per_page - 1) /
1010                                          BBS_user_limit_per_page;                                          BBS_user_limit_per_page;
1011    
1012          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
# Line 851  int query_user_list(int page_id, USER_IN Line 1017  int query_user_list(int page_id, USER_IN
1017          }          }
1018    
1019          *p_user_count = MIN(BBS_user_limit_per_page,          *p_user_count = MIN(BBS_user_limit_per_page,
1020                                                  p_user_list_pool->p_current->user_count -                                                  p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count -
1021                                                          page_id * BBS_user_limit_per_page);                                                          page_id * BBS_user_limit_per_page);
1022    
1023          memcpy(p_users,          memcpy(p_users,
1024                     p_user_list_pool->p_current->users + page_id * BBS_user_limit_per_page,                     p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users + page_id * BBS_user_limit_per_page,
1025                     sizeof(USER_INFO) * (size_t)(*p_user_count));                     sizeof(USER_INFO) * (size_t)(*p_user_count));
1026    
1027  cleanup:  cleanup:
1028          // release lock of user list          // release lock of user list
1029          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1030          {          {
1031                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1032                  ret = -1;                  ret = -1;
# Line 883  int query_user_online_list(int page_id, Line 1049  int query_user_online_list(int page_id,
1049          *p_page_count = 0;          *p_page_count = 0;
1050    
1051          // acquire lock of user list          // acquire lock of user list
1052          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1053          {          {
1054                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1055                  return -2;                  return -2;
1056          }          }
1057    
1058          if (p_user_list_pool->p_online_current->user_count == 0)          if (p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count == 0)
1059          {          {
1060                  // empty list                  // empty list
1061                  ret = 0;                  ret = 0;
1062                  goto cleanup;                  goto cleanup;
1063          }          }
1064    
1065          *p_page_count = (p_user_list_pool->p_online_current->user_count + BBS_user_limit_per_page - 1) / BBS_user_limit_per_page;          *p_page_count = (p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count + BBS_user_limit_per_page - 1) / BBS_user_limit_per_page;
1066    
1067          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
1068          {          {
# Line 906  int query_user_online_list(int page_id, Line 1072  int query_user_online_list(int page_id,
1072          }          }
1073    
1074          *p_user_count = MIN(BBS_user_limit_per_page,          *p_user_count = MIN(BBS_user_limit_per_page,
1075                                                  p_user_list_pool->p_online_current->user_count -                                                  p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count -
1076                                                          page_id * BBS_user_limit_per_page);                                                          page_id * BBS_user_limit_per_page);
1077    
1078          memcpy(p_online_users,          memcpy(p_online_users,
1079                     p_user_list_pool->p_online_current->users + page_id * BBS_user_limit_per_page,                     p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users + page_id * BBS_user_limit_per_page,
1080                     sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));                     sizeof(USER_ONLINE_INFO) * (size_t)(*p_user_count));
1081    
1082  cleanup:  cleanup:
1083          // release lock of user list          // release lock of user list
1084          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1085          {          {
1086                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1087                  ret = -1;                  ret = -1;
# Line 933  int get_user_list_count(int *p_user_cnt) Line 1099  int get_user_list_count(int *p_user_cnt)
1099          }          }
1100    
1101          // acquire lock of user list          // acquire lock of user list
1102          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1103          {          {
1104                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1105                  return -2;                  return -2;
1106          }          }
1107    
1108          *p_user_cnt = p_user_list_pool->p_current->user_count;          *p_user_cnt = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count;
1109    
1110          // release lock of user list          // release lock of user list
1111          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1112          {          {
1113                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1114                  return -2;                  return -2;
# Line 960  int get_user_online_list_count(int *p_us Line 1126  int get_user_online_list_count(int *p_us
1126          }          }
1127    
1128          // acquire lock of user list          // acquire lock of user list
1129          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1130          {          {
1131                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1132                  return -2;                  return -2;
1133          }          }
1134    
1135          *p_user_cnt = p_user_list_pool->p_online_current->user_count;          *p_user_cnt = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count;
1136          *p_guest_cnt = p_user_list_pool->p_online_current->guest_count;          *p_guest_cnt = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].guest_count;
1137    
1138          // release lock of user list          // release lock of user list
1139          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1140          {          {
1141                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1142                  return -2;                  return -2;
# Line 1003  int query_user_info(int32_t id, USER_INF Line 1169  int query_user_info(int32_t id, USER_INF
1169          }          }
1170    
1171          // acquire lock of user list          // acquire lock of user list
1172          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1173          {          {
1174                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1175                  return -2;                  return -2;
1176          }          }
1177    
1178          if (id >= 0 && id < p_user_list_pool->p_current->user_count) // Found          if (id >= 0 && id < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count) // Found
1179          {          {
1180                  *p_user = p_user_list_pool->p_current->users[id];                  *p_user = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id];
1181                  ret = 1;                  ret = 1;
1182          }          }
1183    
1184          // release lock of user list          // release lock of user list
1185          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1186          {          {
1187                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1188                  ret = -1;                  ret = -1;
# Line 1040  int query_user_info_by_uid(int32_t uid, Line 1206  int query_user_info_by_uid(int32_t uid,
1206          }          }
1207    
1208          // acquire lock of user list          // acquire lock of user list
1209          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1210          {          {
1211                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1212                  return -2;                  return -2;
1213          }          }
1214    
1215          left = 0;          left = 0;
1216          right = p_user_list_pool->p_current->user_count - 1;          right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1217    
1218          while (left < right)          while (left < right)
1219          {          {
1220                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1221                  if (uid < p_user_list_pool->p_current->index_uid[mid].uid)                  if (uid < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1222                  {                  {
1223                          right = mid - 1;                          right = mid - 1;
1224                  }                  }
1225                  else if (uid > p_user_list_pool->p_current->index_uid[mid].uid)                  else if (uid > p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1226                  {                  {
1227                          left = mid + 1;                          left = mid + 1;
1228                  }                  }
1229                  else // if (uid == p_user_list_pool->p_current->index_uid[mid].uid)                  else // if (uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1230                  {                  {
1231                          left = mid;                          left = mid;
1232                          break;                          break;
1233                  }                  }
1234          }          }
1235    
1236          if (uid == p_user_list_pool->p_current->index_uid[left].uid) // Found          if (uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left].uid) // Found
1237          {          {
1238                  id = p_user_list_pool->p_current->index_uid[left].id;                  id = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left].id;
1239                  *p_user = p_user_list_pool->p_current->users[id];                  *p_user = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id];
1240                  ret = 1;                  ret = 1;
1241    
1242                  if (p_intro_buf != NULL)                  if (p_intro_buf != NULL)
1243                  {                  {
1244                          strncpy(p_intro_buf, p_user_list_pool->p_current->users[id].intro, intro_buf_len - 1);                          strncpy(p_intro_buf, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[id].intro, intro_buf_len - 1);
1245                          p_intro_buf[intro_buf_len - 1] = '\0';                          p_intro_buf[intro_buf_len - 1] = '\0';
1246                          p_user->intro = p_intro_buf;                          p_user->intro = p_intro_buf;
1247                  }                  }
1248          }          }
1249    
1250          // release lock of user list          // release lock of user list
1251          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1252          {          {
1253                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1254                  ret = -1;                  ret = -1;
# Line 1112  int query_user_info_by_username(const ch Line 1278  int query_user_info_by_username(const ch
1278          prefix_len = strlen(username_prefix);          prefix_len = strlen(username_prefix);
1279    
1280          // acquire lock of user list          // acquire lock of user list
1281          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1282          {          {
1283                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1284                  return -2;                  return -2;
1285          }          }
1286    
1287          left = 0;          left = 0;
1288          right = p_user_list_pool->p_current->user_count - 1;          right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1289    
1290          while (left < right)          while (left < right)
1291          {          {
1292                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1293                  comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len);                  comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len);
1294                  if (comp < 0)                  if (comp < 0)
1295                  {                  {
1296                          right = mid - 1;                          right = mid - 1;
# Line 1140  int query_user_info_by_username(const ch Line 1306  int query_user_info_by_username(const ch
1306                  }                  }
1307          }          }
1308    
1309          if (strncasecmp(username_prefix, p_user_list_pool->p_current->users[left].username, prefix_len) == 0) // Found          if (strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left].username, prefix_len) == 0) // Found
1310          {          {
1311  #ifdef _DEBUG  #ifdef _DEBUG
1312                  log_error("Debug: match found, pos=%d\n", left);                  log_error("Debug: match found, pos=%d\n", left);
# Line 1153  int query_user_info_by_username(const ch Line 1319  int query_user_info_by_username(const ch
1319                  while (left < right)                  while (left < right)
1320                  {                  {
1321                          mid = (left + right) / 2;                          mid = (left + right) / 2;
1322                          comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len);                          comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len);
1323                          if (comp > 0)                          if (comp > 0)
1324                          {                          {
1325                                  left = mid + 1;                                  left = mid + 1;
# Line 1176  int query_user_info_by_username(const ch Line 1342  int query_user_info_by_username(const ch
1342    
1343                  left = left_save;                  left = left_save;
1344                  left_save = right;                  left_save = right;
1345                  right = p_user_list_pool->p_current->user_count - 1;                  right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1346    
1347                  while (left < right)                  while (left < right)
1348                  {                  {
1349                          mid = (left + right) / 2 + (left + right) % 2;                          mid = (left + right) / 2 + (left + right) % 2;
1350                          comp = strncasecmp(username_prefix, p_user_list_pool->p_current->users[mid].username, prefix_len);                          comp = strncasecmp(username_prefix, p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[mid].username, prefix_len);
1351                          if (comp < 0)                          if (comp < 0)
1352                          {                          {
1353                                  right = mid - 1;                                  right = mid - 1;
# Line 1207  int query_user_info_by_username(const ch Line 1373  int query_user_info_by_username(const ch
1373    
1374                  for (i = 0; i < max_user_cnt && left + i <= right; i++)                  for (i = 0; i < max_user_cnt && left + i <= right; i++)
1375                  {                  {
1376                          uid_list[i] = p_user_list_pool->p_current->users[left + i].uid;                          uid_list[i] = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left + i].uid;
1377                          strncpy(username_list[i], p_user_list_pool->p_current->users[left + i].username, sizeof(username_list[i]) - 1);                          strncpy(username_list[i], p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].users[left + i].username, sizeof(username_list[i]) - 1);
1378                          username_list[i][sizeof(username_list[i]) - 1] = '\0';                          username_list[i][sizeof(username_list[i]) - 1] = '\0';
1379                  }                  }
1380                  ret = i;                  ret = i;
# Line 1216  int query_user_info_by_username(const ch Line 1382  int query_user_info_by_username(const ch
1382    
1383  cleanup:  cleanup:
1384          // release lock of user list          // release lock of user list
1385          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1386          {          {
1387                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1388                  ret = -1;                  ret = -1;
# Line 1236  int query_user_online_info(int32_t id, U Line 1402  int query_user_online_info(int32_t id, U
1402          }          }
1403    
1404          // acquire lock of user list          // acquire lock of user list
1405          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1406          {          {
1407                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1408                  return -2;                  return -2;
1409          }          }
1410    
1411          if (id >= 0 && id < p_user_list_pool->p_online_current->user_count) // Found          if (id >= 0 && id < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count) // Found
1412          {          {
1413                  *p_user = p_user_list_pool->p_online_current->users[id];                  *p_user = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users[id];
1414                  ret = 1;                  ret = 1;
1415          }          }
1416    
1417          // release lock of user list          // release lock of user list
1418          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1419          {          {
1420                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1421                  ret = -1;                  ret = -1;
# Line 1278  int query_user_online_info_by_uid(int32_ Line 1444  int query_user_online_info_by_uid(int32_
1444          *p_user_cnt = 0;          *p_user_cnt = 0;
1445    
1446          // acquire lock of user list          // acquire lock of user list
1447          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1448          {          {
1449                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1450                  return -2;                  return -2;
1451          }          }
1452    
1453          left = start_id;          left = start_id;
1454          right = p_user_list_pool->p_online_current->user_count - 1;          right = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count - 1;
1455    
1456          while (left < right)          while (left < right)
1457          {          {
1458                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1459                  if (uid < p_user_list_pool->p_online_current->index_uid[mid].uid)                  if (uid < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1460                  {                  {
1461                          right = mid - 1;                          right = mid - 1;
1462                  }                  }
1463                  else if (uid > p_user_list_pool->p_online_current->index_uid[mid].uid)                  else if (uid > p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1464                  {                  {
1465                          left = mid + 1;                          left = mid + 1;
1466                  }                  }
1467                  else // if (uid == p_user_list_pool->p_online_current->index_uid[mid].uid)                  else // if (uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1468                  {                  {
1469                          left = mid;                          left = mid;
1470                          break;                          break;
1471                  }                  }
1472          }          }
1473    
1474          if (uid == p_user_list_pool->p_online_current->index_uid[left].uid)          if (uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].uid)
1475          {          {
1476                  right = left;                  right = left;
1477                  left = start_id;                  left = start_id;
# Line 1313  int query_user_online_info_by_uid(int32_ Line 1479  int query_user_online_info_by_uid(int32_
1479                  while (left < right)                  while (left < right)
1480                  {                  {
1481                          mid = (left + right) / 2;                          mid = (left + right) / 2;
1482                          if (uid - 1 < p_user_list_pool->p_online_current->index_uid[mid].uid)                          if (uid - 1 < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1483                          {                          {
1484                                  right = mid;                                  right = mid;
1485                          }                          }
1486                          else // if (uid - 1 >= p_user_list_pool->p_online_current->index_uid[mid].uid)                          else // if (uid - 1 >= p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[mid].uid)
1487                          {                          {
1488                                  left = mid + 1;                                  left = mid + 1;
1489                          }                          }
1490                  }                  }
1491    
1492                  for (i = 0;                  for (i = 0;
1493                           left < p_user_list_pool->p_online_current->user_count && i < user_cnt &&                           left < p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].user_count && i < user_cnt &&
1494                           uid == p_user_list_pool->p_online_current->index_uid[left].uid;                           uid == p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].uid;
1495                           left++, i++)                           left++, i++)
1496                  {                  {
1497                          id = p_user_list_pool->p_online_current->index_uid[left].id;                          id = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].index_uid[left].id;
1498                          p_users[i] = p_user_list_pool->p_online_current->users[id];                          p_users[i] = p_user_list_pool->user_online_list[p_user_list_pool->user_online_list_index_current].users[id];
1499                  }                  }
1500    
1501                  if (i > 0)                  if (i > 0)
# Line 1340  int query_user_online_info_by_uid(int32_ Line 1506  int query_user_online_info_by_uid(int32_
1506          }          }
1507    
1508          // release lock of user list          // release lock of user list
1509          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1510          {          {
1511                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1512                  ret = -1;                  ret = -1;
# Line 1364  int get_user_id_list(int32_t *p_uid_list Line 1530  int get_user_id_list(int32_t *p_uid_list
1530          }          }
1531    
1532          // acquire lock of user list          // acquire lock of user list
1533          if (user_list_rd_lock(p_user_list_pool->semid) < 0)          if (user_list_rd_lock() < 0)
1534          {          {
1535                  log_error("user_list_rd_lock() error\n");                  log_error("user_list_rd_lock() error\n");
1536                  return -2;                  return -2;
1537          }          }
1538    
1539          left = 0;          left = 0;
1540          right = p_user_list_pool->p_current->user_count - 1;          right = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count - 1;
1541    
1542          while (left < right)          while (left < right)
1543          {          {
1544                  mid = (left + right) / 2;                  mid = (left + right) / 2;
1545                  if (start_uid < p_user_list_pool->p_current->index_uid[mid].uid)                  if (start_uid < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1546                  {                  {
1547                          right = mid - 1;                          right = mid - 1;
1548                  }                  }
1549                  else if (start_uid > p_user_list_pool->p_current->index_uid[mid].uid)                  else if (start_uid > p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1550                  {                  {
1551                          left = mid + 1;                          left = mid + 1;
1552                  }                  }
1553                  else // if (start_uid == p_user_list_pool->p_current->index_uid[mid].uid)                  else // if (start_uid == p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[mid].uid)
1554                  {                  {
1555                          left = mid;                          left = mid;
1556                          break;                          break;
1557                  }                  }
1558          }          }
1559    
1560          for (i = 0; i < *p_user_cnt && left + i < p_user_list_pool->p_current->user_count; i++)          for (i = 0; i < *p_user_cnt && left + i < p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].user_count; i++)
1561          {          {
1562                  p_uid_list[i] = p_user_list_pool->p_current->index_uid[left + i].uid;                  p_uid_list[i] = p_user_list_pool->user_list[p_user_list_pool->user_list_index_current].index_uid[left + i].uid;
1563          }          }
1564          *p_user_cnt = i;          *p_user_cnt = i;
1565    
1566          // release lock of user list          // release lock of user list
1567          if (user_list_rd_unlock(p_user_list_pool->semid) < 0)          if (user_list_rd_unlock() < 0)
1568          {          {
1569                  log_error("user_list_rd_unlock() error\n");                  log_error("user_list_rd_unlock() error\n");
1570                  ret = -1;                  ret = -1;


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

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