修改线程获取缓冲区失败问题。

修改共享内存地址映射失败时的判错条件,shmat失败会返回(void *)-1而不是NULL
This commit is contained in:
guo_peixu
2022-06-23 14:20:32 +08:00
parent 4531d74c80
commit fada178df2
2 changed files with 25 additions and 26 deletions

View File

@@ -529,7 +529,8 @@ int main(int argc, char **argv)
} }
if(ring_queue_head[i] == NULL){ if(ring_queue_head[i] == NULL){
ring_queue_head[i] = shmat(tmp_ovw->shmid, NULL, 0); ring_queue_head[i] = shmat(tmp_ovw->shmid, NULL, 0);
if(ring_queue_head[i] == NULL){ if(ring_queue_head[i] == (struct MESA_shm_queue_head *)-1){
ring_queue_head[i] = NULL;
break ; break ;
} }
} }

View File

@@ -37,7 +37,7 @@ struct MESA_shm_overview *MESA_shm_alloc_overview()
return NULL; return NULL;
}else{ }else{
shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0); shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0);
if(shm_overview == NULL){ if(shm_overview == (struct MESA_shm_overview *)-1){
return NULL; return NULL;
} }
memset((void *)shm_overview, 0, shmsize); memset((void *)shm_overview, 0, shmsize);
@@ -51,6 +51,9 @@ struct MESA_shm_overview *MESA_shm_alloc_overview()
} }
}else{ }else{
shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0); shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0);
if(shm_overview == (struct MESA_shm_overview *)-1){
return NULL;
}
} }
return shm_overview; return shm_overview;
} }
@@ -100,9 +103,6 @@ struct MESA_shm_queue_head *MESA_shm_alloc_new_ring_queue(struct MESA_shm_overvi
struct MESA_shm_queue_head *head = NULL; struct MESA_shm_queue_head *head = NULL;
int shmid = -1; int shmid = -1;
int shmsize = sizeof(struct MESA_shm_queue_head) + (MESA_SHM_RING_QUEUE_BLOCK_NUM * MESA_SHM_RING_QUEUE_BLOCK_SIZE); int shmsize = sizeof(struct MESA_shm_queue_head) + (MESA_SHM_RING_QUEUE_BLOCK_NUM * MESA_SHM_RING_QUEUE_BLOCK_SIZE);
if(!MESA_shm_mutex_trylock_success(ovw)){
return NULL;
}
shmid = shmget(ovw->shmkey, 0, 0); shmid = shmget(ovw->shmkey, 0, 0);
if(shmid == -1){ if(shmid == -1){
shmid = shmget(ovw->shmkey, shmsize, (SHM_R|SHM_W|IPC_CREAT)); shmid = shmget(ovw->shmkey, shmsize, (SHM_R|SHM_W|IPC_CREAT));
@@ -113,31 +113,20 @@ struct MESA_shm_queue_head *MESA_shm_alloc_new_ring_queue(struct MESA_shm_overvi
}else{ }else{
head = (struct MESA_shm_queue_head *)shmat(shmid, NULL, 0); head = (struct MESA_shm_queue_head *)shmat(shmid, NULL, 0);
} }
if(head != NULL){ if(head == (struct MESA_shm_queue_head *)-1){
return NULL;
}
MESA_shm_init_new_ring_queue(head, ovw); MESA_shm_init_new_ring_queue(head, ovw);
ovw->shmid = shmid; ovw->shmid = shmid;
}else{
pthread_mutex_unlock(&ovw->mutex);
}
return head; return head;
} }
struct MESA_shm_queue_head *MESA_shm_try_ring_queue(struct MESA_shm_overview *ovw) struct MESA_shm_queue_head *MESA_shm_try_reuse_ring_queue(struct MESA_shm_overview *ovw)
{ {
struct MESA_shm_queue_head *head = NULL; struct MESA_shm_queue_head *head = NULL;
if(ovw->shmid == -1){
return NULL;
}
if(!MESA_shm_mutex_trylock_success(ovw)){
return NULL;
}
head = (struct MESA_shm_queue_head *)shmat(ovw->shmid, NULL, 0); head = (struct MESA_shm_queue_head *)shmat(ovw->shmid, NULL, 0);
if(head == NULL){ if(head == (struct MESA_shm_queue_head *)-1){
pthread_mutex_unlock(&ovw->mutex); return NULL;
}
if(!MESA_shm_ring_queue_is_empty(head)){
pthread_mutex_unlock(&ovw->mutex);
head == NULL;
} }
return head; return head;
} }
@@ -151,13 +140,22 @@ struct MESA_shm_queue_head *MESA_shm_get_ring_queue(int pid)
} }
for(i = 0; i < MESA_SHM_RING_QUEUE_NUM; i++){ for(i = 0; i < MESA_SHM_RING_QUEUE_NUM; i++){
tmp_ovw = MESA_shm_ovw + i; tmp_ovw = MESA_shm_ovw + i;
if(!MESA_shm_mutex_trylock_success(tmp_ovw)){
continue ;
}
if(tmp_ovw->shmid == -1){ if(tmp_ovw->shmid == -1){
head = MESA_shm_alloc_new_ring_queue(tmp_ovw); head = MESA_shm_alloc_new_ring_queue(tmp_ovw);
if(head == NULL){
pthread_mutex_unlock(&tmp_ovw->mutex);
}
break; break;
} }
head = MESA_shm_try_ring_queue(tmp_ovw); head = MESA_shm_try_reuse_ring_queue(tmp_ovw);
if(head != NULL){ if(head != NULL){
break ; break ;
}else{
pthread_mutex_unlock(&tmp_ovw->mutex);
continue ;
} }
} }
if(head != NULL){ if(head != NULL){