Changeset 6363
- Timestamp:
- 07/01/08 02:41:30 (3 months ago)
- Location:
- trunk/licq
- Files:
-
- 2 modified
-
include/licq_user.h (modified) (3 diffs)
-
src/user.cpp (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/licq/include/licq_user.h
r6362 r6363 234 234 class LicqGroup; 235 235 236 typedef std::pair<std::string, unsigned long> UserMapKey; 237 typedef std::map<UserMapKey, class ICQUser*> UserMap; 236 238 typedef std::list<ICQUser *> UserList; 237 239 typedef std::list<class ICQOwner *> OwnerList; … … 1152 1154 }; 1153 1155 1154 1155 //=====CUsers===================================================================1156 1157 class CUserHashTable1158 {1159 public:1160 CUserHashTable(unsigned short _nSize);1161 ~CUserHashTable();1162 1163 // For protocol plugins1164 ICQUser *Retrieve(const char *, unsigned long);1165 void Store(ICQUser *, const char *, unsigned long);1166 void Remove(const char *, unsigned long);1167 1168 protected:1169 // For protocol plugin1170 unsigned short HashValue(const char *);1171 1172 void Lock(unsigned short _nLockType);1173 void Unlock();1174 1175 std::vector < UserList > m_vlTable;1176 1177 pthread_rdwr_t mutex_rw;1178 unsigned short m_nLockType;1179 };1180 1181 1156 /** 1182 1157 * Class holding data for a user group in the contact list. … … 1512 1487 GroupMap myGroups; 1513 1488 UserList m_vpcUsers; 1489 UserMap myUsers; 1514 1490 OwnerList m_vpcOwners; 1515 CUserHashTable m_hUsers;1516 1491 ICQOwner *m_xOwner; 1517 1492 unsigned long m_nOwnerUin; -
trunk/licq/src/user.cpp
r6362 r6363 487 487 //=====CUserManager============================================================= 488 488 CUserManager::CUserManager() 489 : m_hUsers(USER_HASH_SIZE), 490 m_szDefaultEncoding(NULL) 489 : m_szDefaultEncoding(NULL) 491 490 { 492 491 // Set up the basic all users and new users group … … 657 656 ICQUser *u; 658 657 usersConf.SetFlags(INI_FxWARN); 658 LockUserList(LOCK_W); 659 659 for (unsigned short i = 1; i<= nUsers; i++) 660 660 { … … 680 680 u = new ICQUser(szId, nPPID, filename); 681 681 u->AddToContactList(); 682 m _hUsers.Store(u, szId, nPPID);682 myUsers[UserMapKey(szId, nPPID)] = u; 683 683 m_vpcUsers.push_back(u); 684 684 } 685 UnlockUserList(); 685 686 686 687 return true; … … 689 690 void CUserManager::AddUser(ICQUser *pUser, const char *_szId, unsigned long _nPPID) 690 691 { 692 LockUserList(LOCK_W); 693 691 694 pUser->Lock(LOCK_W); 692 695 … … 710 713 } 711 714 712 // Store the user in the hash table713 m _hUsers.Store(pUser, _szId, _nPPID);715 // Store the user in the lookup map 716 myUsers[UserMapKey(_szId, _nPPID)] = pUser; 714 717 // Reorder the user to the correct place 715 718 m_vpcUsers.push_back(pUser); 719 720 UnlockUserList(); 716 721 } 717 722 … … 732 737 m_vpcUsers.erase(iter); 733 738 DropUser(u); 734 m _hUsers.Remove(_szId, _nPPID);739 myUsers.erase(UserMapKey(_szId, _nPPID)); 735 740 UnlockUserList(); 736 741 delete u; … … 768 773 769 774 if (u == NULL) 770 u = m_hUsers.Retrieve(_szId, _nPPID); 775 { 776 LockUserList(LOCK_R); 777 UserMap::iterator iter = myUsers.find(UserMapKey(_szId, _nPPID)); 778 if (iter != myUsers.end()) 779 u = iter->second; 780 UnlockUserList(); 781 } 771 782 772 783 if (u != NULL) … … 788 799 bool CUserManager::IsOnList(const char *_szId, unsigned long _nPPID) 789 800 { 790 if (FindOwner(_szId, _nPPID)) return true; 791 return m_hUsers.Retrieve(_szId, _nPPID) != NULL; 801 if (FindOwner(_szId, _nPPID)) 802 return true; 803 804 LockUserList(LOCK_R); 805 UserMap::iterator iter = myUsers.find(UserMapKey(_szId, _nPPID)); 806 bool found = (iter != myUsers.end()); 807 UnlockUserList(); 808 809 return found; 792 810 } 793 811 … … 1646 1664 SetString(&m_szDefaultEncoding, defaultEncoding); 1647 1665 } 1648 1649 1650 //=====CUserHashTable===========================================================1651 CUserHashTable::CUserHashTable(unsigned short _nSize) : m_vlTable(_nSize)1652 {1653 pthread_rdwr_init_np(&mutex_rw, NULL);1654 pthread_rdwr_set_name(&mutex_rw, __func__);1655 m_nLockType = LOCK_R;1656 }1657 1658 CUserHashTable::~CUserHashTable()1659 {1660 pthread_rdwr_destroy_np(&mutex_rw);1661 }1662 1663 ICQUser *CUserHashTable::Retrieve(const char *_szId, unsigned long _nPPID)1664 {1665 Lock(LOCK_R);1666 1667 ICQUser *u = NULL;1668 char *szRealId;1669 ICQUser::MakeRealId(_szId, _nPPID, szRealId);1670 UserList &l = m_vlTable[HashValue(szRealId)];1671 char *szTempId = 0;1672 unsigned long nPPID;1673 UserList::iterator iter;1674 for (iter = l.begin(); iter != l.end(); ++iter)1675 {1676 nPPID = (*iter)->PPID();1677 ICQUser::MakeRealId((*iter)->IdString(), nPPID, szTempId);1678 if (_nPPID == nPPID && strcasecmp(szTempId, szRealId) == 0)1679 {1680 u = (*iter);1681 break;1682 }1683 delete [] szTempId; szTempId = 0;1684 }1685 if (iter == l.end()) u = NULL;1686 1687 delete [] szRealId;1688 if (szTempId) delete [] szTempId;1689 1690 Unlock();1691 return u;1692 }1693 1694 void CUserHashTable::Store(ICQUser *u, const char *_szId, unsigned long _nPPID)1695 {1696 Lock(LOCK_W);1697 char *szRealId;1698 ICQUser::MakeRealId(_szId, _nPPID, szRealId);1699 UserList &l = m_vlTable[HashValue(szRealId)];1700 delete [] szRealId;1701 l.push_front(u);1702 Unlock();1703 }1704 1705 void CUserHashTable::Remove(const char *_szId, unsigned long _nPPID)1706 {1707 Lock(LOCK_W);1708 1709 char *szRealId;1710 ICQUser::MakeRealId(_szId, _nPPID, szRealId);1711 UserList &l = m_vlTable[HashValue(szRealId)];1712 delete [] szRealId;1713 char *szId;1714 unsigned long nPPID;1715 UserList::iterator iter;1716 for (iter = l.begin(); iter != l.end(); ++iter)1717 {1718 (*iter)->Lock(LOCK_R);1719 szId = (*iter)->IdString();1720 nPPID = (*iter)->PPID();1721 (*iter)->Unlock();1722 if (_nPPID == nPPID && strcmp(szId, _szId) == 0)1723 {1724 l.erase(iter);1725 break;1726 }1727 }1728 1729 Unlock();1730 }1731 1732 unsigned short CUserHashTable::HashValue(const char *_szId)1733 {1734 int j = strlen(_szId);1735 unsigned short nRet = 0;1736 for (int i = 0; i < j; i++)1737 nRet += (unsigned short)_szId[i];1738 1739 return (nRet % (USER_HASH_SIZE - 1));1740 }1741 1742 void CUserHashTable::Lock(unsigned short _nLockType)1743 {1744 switch (_nLockType)1745 {1746 case LOCK_R:1747 pthread_rdwr_rlock_np (&mutex_rw);1748 break;1749 case LOCK_W:1750 pthread_rdwr_wlock_np(&mutex_rw);1751 break;1752 default:1753 assert(false);1754 return;1755 }1756 m_nLockType = _nLockType;1757 }1758 1759 void CUserHashTable::Unlock()1760 {1761 unsigned short nLockType = m_nLockType;1762 m_nLockType = LOCK_R;1763 switch (nLockType)1764 {1765 case LOCK_R:1766 pthread_rdwr_runlock_np(&mutex_rw);1767 break;1768 case LOCK_W:1769 pthread_rdwr_wunlock_np(&mutex_rw);1770 break;1771 default:1772 assert(false);1773 break;1774 }1775 }1776 1777 1666 1778 1667
