Browse Source

Z991239-5812 #comment 优化UOS共享内存库

80274480 11 months ago
parent
commit
75a2427d97
1 changed files with 58 additions and 57 deletions
  1. 58 57
      Other/libsharememory/libsharememory.cpp

+ 58 - 57
Other/libsharememory/libsharememory.cpp

@@ -37,11 +37,9 @@ protected:
 public:
 	libsharememory_impl()
 	{
-
 #ifdef _WIN32
 		m_hMutex = NULL;
 		m_hFileMap = NULL;
-		
 #else
 		m_shid = -1;
 		m_semt = SEM_FAILED;
@@ -98,41 +96,47 @@ public:
 		}
 #else
 		if (szName != NULL && dwBytes > 0) {
-            snprintf(m_strshmname, MAX_PATH, "%s", szName);
-            snprintf(m_strsemname, MAX_PATH, "/mutex.%s", szName);
-			mode_t fm = umask(0);
-            m_shid = shm_open(m_strshmname, O_CREAT | O_TRUNC | O_RDWR, 0777);
-			umask(fm);	
-            if (m_shid >= 0) {
-                //aos_debug_log((LB, "shm_open O_CREAT | O_TRUNC | O_RDWR, 0777) success and share memory %s and size is %d", m_strshmname, dwBytes));
-            } else {
-                //aos_error_log((LB, "shm_open O_CREAT | O_TRUNC | O_RDWR, 0777) %s failed for %s, and errno is %d", m_strshmname, strerror(errno), errno));
-                if (EEXIST == errno) {
-                    //m_shid = shm_open(m_strshmname, O_RDWR, 0);
-                }
-            }
-            if (m_shid != -1) {
-                ftruncate(m_shid, dwBytes);
-                m_pData = mmap(NULL, dwBytes, PROT_READ | PROT_WRITE, MAP_SHARED, m_shid, 0);
-                if (MAP_FAILED != m_pData) {
-                    *((unsigned int*)m_pData) = dwBytes;
-                    m_dwBytes = dwBytes;
-					m_semt = sem_open(m_strsemname, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, 1);
-                    if (SEM_FAILED == m_semt && EEXIST == errno) {
-						m_semt = sem_open(m_strsemname, 0);
-                        sem_init(m_semt, 32, 1);
-                        int ivalue = -1;
-                        sem_getvalue(m_semt, &ivalue);
-						bRet = true;
-					} else if (SEM_FAILED != m_semt) {
-                        int ivalue = -1;
-                        sem_getvalue(m_semt, &ivalue);
-						bRet = true;
-					} 
-                } 
-            }
-			if (!bRet) {
-				CloseShareMem();
+			char szBuf[MAX_PATH] = { 0 };
+			const char* szFile = tmpnam(szBuf);
+			if (szFile != NULL) {
+				snprintf(m_strshmname, MAX_PATH, "%s", szName);
+				snprintf(m_strsemname, MAX_PATH, "/mutex.%s", szName);
+				m_shid = shm_open(szName, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
+				if (m_shid >= 0) {
+					//Debug("%s:%d shm_open O_CREAT|O_RDWR|O_EXCL success and share memory %s and size is %d", __FUNCTION__, __LINE__, szName, dwBytes);
+				}
+				else {
+					//Debug("%s:%d shm_open O_CREAT|O_RDWR|O_EXCL %s failed for %s, and errno is %d", __FUNCTION__, __LINE__, m_strshmname, strerror(errno), errno);
+					if (EEXIST == errno) {
+						m_shid = shm_open(szName, O_RDWR, 0);
+					}
+				}
+
+				if (m_shid >= 0) {
+					if (NULL == m_pData) {
+						ftruncate(m_shid, dwBytes);
+						m_pData = mmap(NULL, dwBytes, PROT_READ | PROT_WRITE, MAP_SHARED, m_shid, 0);
+						if (MAP_FAILED != m_pData) {
+							*((unsigned int*)m_pData) = dwBytes;
+							m_dwBytes = dwBytes;
+							sem_t* semt = sem_open(m_strsemname, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, 1);
+							if (NULL == semt) {
+								if (EEXIST == errno) {
+									semt = sem_open(m_strsemname, 0);
+									sem_init(semt, 32, 1);
+								}
+							}
+
+
+							if (NULL != semt) {
+								m_semt = semt;
+								int ivalue = -1;
+								sem_getvalue(m_semt, &ivalue);
+								bRet = true;
+							}
+						}
+					}
+				}
 			}
 		}
 #endif // _WIN32
@@ -142,7 +146,7 @@ public:
 	//打开已有的共享内存区
 	bool OpenShareMem(const char* szName)
 	{
-		bool T=false;
+		bool bRet = false;
 #ifdef _WIN32
 		char szBuf[MAX_PATH];
 		if (szName != NULL)
@@ -164,48 +168,45 @@ public:
 						UnlockShareMem();
 						if (m_dwBytes > 0)
 						{
-							T = true;
+							bRet = true;
 						}
 					}
 				}
 			}
-			if (!T)
+			if (!bRet)
 			{
 				CloseShareMem();
 			}
 		}
 #else
 		if (szName != NULL) {
-			snprintf(m_strshmname, MAX_PATH, "%s", szName);
-			snprintf(m_strsemname, MAX_PATH, "/mutex.%s", szName);
-			mode_t fm = umask(0);
-			m_shid = shm_open(m_strshmname, O_RDWR, 0777);
-			umask(fm);
-			if (m_shid != -1) {
-				m_semt = sem_open(m_strsemname, 0);
-				if (SEM_FAILED != m_semt) {
+			int shid = shm_open(szName, O_RDWR, 0);
+			if (-1 != shid) {
+				m_shid = shid;
+				sem_t* semt = sem_open(m_strsemname, 0);
+				if (SEM_FAILED != semt){
+					m_semt = semt;
 					int ivalue = -1;
-					sem_getvalue(m_semt, &ivalue);
-					lseek(m_shid, 0, SEEK_SET);
-					m_pData = mmap(NULL, m_dwBytes, PROT_READ | PROT_WRITE, MAP_SHARED, m_shid, 0);
+					sem_getvalue(semt, &ivalue);
+					m_pData = mmap(NULL, m_dwBytes, PROT_READ | PROT_WRITE, MAP_SHARED, shid, 0);
 					if (MAP_FAILED != m_pData) {
-						if (LockShareMem()) {
+						if (LockShareMem()){
 							m_dwBytes = *((unsigned int*)m_pData);
 							UnlockShareMem();
-							if (m_dwBytes > 0) {
-								T = true;
+							if (m_dwBytes > 0){
+								bRet = true;
 							}
 						}
-					} 
+					}
 				}
 			}
 			
-			if (!T){
+			if (!bRet){
 				CloseShareMem();
 			}
 		}
 #endif // RVC_OS_WIN
-		return T;
+		return bRet;
 	}
 
 	//释放共享内存区