From fada178df2c79f2a3a5fa5accd51d026fdeaf77c Mon Sep 17 00:00:00 2001 From: guo_peixu Date: Thu, 23 Jun 2022 14:20:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BA=BF=E7=A8=8B=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=BC=93=E5=86=B2=E5=8C=BA=E5=A4=B1=E8=B4=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82=20=E4=BF=AE=E6=94=B9=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E5=86=85=E5=AD=98=E5=9C=B0=E5=9D=80=E6=98=A0=E5=B0=84=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=97=B6=E7=9A=84=E5=88=A4=E9=94=99=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=EF=BC=8Cshmat=E5=A4=B1=E8=B4=A5=E4=BC=9A=E8=BF=94=E5=9B=9E(voi?= =?UTF-8?q?d=20*)-1=E8=80=8C=E4=B8=8D=E6=98=AFNULL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shm_consumer/MESA_shm_consumer.c | 3 +- src/MESA_shm_ring_queue.c | 48 +++++++++++++++----------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/shm_consumer/MESA_shm_consumer.c b/shm_consumer/MESA_shm_consumer.c index 5bdbfe6..9e4aba3 100644 --- a/shm_consumer/MESA_shm_consumer.c +++ b/shm_consumer/MESA_shm_consumer.c @@ -529,7 +529,8 @@ int main(int argc, char **argv) } if(ring_queue_head[i] == NULL){ 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 ; } } diff --git a/src/MESA_shm_ring_queue.c b/src/MESA_shm_ring_queue.c index 78fd6f9..aaaafd2 100644 --- a/src/MESA_shm_ring_queue.c +++ b/src/MESA_shm_ring_queue.c @@ -37,7 +37,7 @@ struct MESA_shm_overview *MESA_shm_alloc_overview() return NULL; }else{ shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0); - if(shm_overview == NULL){ + if(shm_overview == (struct MESA_shm_overview *)-1){ return NULL; } memset((void *)shm_overview, 0, shmsize); @@ -51,6 +51,9 @@ struct MESA_shm_overview *MESA_shm_alloc_overview() } }else{ shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0); + if(shm_overview == (struct MESA_shm_overview *)-1){ + return NULL; + } } 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; int shmid = -1; 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); if(shmid == -1){ 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{ head = (struct MESA_shm_queue_head *)shmat(shmid, NULL, 0); } - if(head != NULL){ - MESA_shm_init_new_ring_queue(head, ovw); - ovw->shmid = shmid; - }else{ - pthread_mutex_unlock(&ovw->mutex); + if(head == (struct MESA_shm_queue_head *)-1){ + return NULL; } + MESA_shm_init_new_ring_queue(head, ovw); + ovw->shmid = shmid; 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; - 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); - if(head == NULL){ - pthread_mutex_unlock(&ovw->mutex); - } - if(!MESA_shm_ring_queue_is_empty(head)){ - pthread_mutex_unlock(&ovw->mutex); - head == NULL; + if(head == (struct MESA_shm_queue_head *)-1){ + return NULL; } return head; } @@ -151,14 +140,23 @@ struct MESA_shm_queue_head *MESA_shm_get_ring_queue(int pid) } for(i = 0; i < MESA_SHM_RING_QUEUE_NUM; i++){ tmp_ovw = MESA_shm_ovw + i; + if(!MESA_shm_mutex_trylock_success(tmp_ovw)){ + continue ; + } if(tmp_ovw->shmid == -1){ head = MESA_shm_alloc_new_ring_queue(tmp_ovw); + if(head == NULL){ + pthread_mutex_unlock(&tmp_ovw->mutex); + } break; } - head = MESA_shm_try_ring_queue(tmp_ovw); + head = MESA_shm_try_reuse_ring_queue(tmp_ovw); if(head != NULL){ - break; - } + break ; + }else{ + pthread_mutex_unlock(&tmp_ovw->mutex); + continue ; + } } if(head != NULL){ tmp_ovw->producer_pid = pid;