From 7910fd0802b3d8d71d5f90af5c93cb6b23ea48d3 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期三, 03 二月 2021 09:53:36 +0800 Subject: [PATCH] update --- src/socket/shm_socket.cpp | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/socket/shm_socket.cpp b/src/socket/shm_socket.cpp index 9544827..690124f 100644 --- a/src/socket/shm_socket.cpp +++ b/src/socket/shm_socket.cpp @@ -148,27 +148,31 @@ hashtable_t *hashtable = mm_get_hashtable(); + if( sockt->queue != NULL) + goto LABEL_PUSH; - if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0) + { + if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0) err_exit(rv, "shm_sendto : pthread_mutex_lock"); - if (sockt->queue == NULL) { - if (sockt->key == 0) { - sockt->key = hashtable_alloc_key(hashtable); + if (sockt->queue == NULL) { + if (sockt->key == 0) { + sockt->key = hashtable_alloc_key(hashtable); + } + sockt->queue = shm_socket_bind_queue( sockt->key, sockt->force_bind); + if(sockt->queue == NULL ) { + logger->error("%s. key = %d", bus_strerror(EBUS_KEY_INUSED), sockt->key); + return EBUS_KEY_INUSED; + } } - sockt->queue = shm_socket_bind_queue( sockt->key, sockt->force_bind); - if(sockt->queue == NULL ) { - logger->error("%s. key = %d", bus_strerror(EBUS_KEY_INUSED), sockt->key); - return EBUS_KEY_INUSED; - } - } - if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0) - err_exit(rv, "shm_sendto : pthread_mutex_unlock"); + if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0) + err_exit(rv, "shm_sendto : pthread_mutex_unlock"); - // There is some case where a sockt need to send to himeself, for example when bus server need to stop, he need to send himself - // a top message. + } + + LABEL_PUSH: if (key == sockt->key) { logger->error( "can not send to your self!"); return EBUS_SENDTO_SELF; @@ -204,16 +208,21 @@ } + // 鐭繛鎺ユ柟寮忔帴鍙� int shm_recvfrom(shm_socket_t *sockt, void **buf, int *size, int *key, const struct timespec *timeout, int flag) { int rv; hashtable_t *hashtable = mm_get_hashtable(); - if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0) - err_exit(rv, "shm_recvfrom : pthread_mutex_lock"); + if( sockt->queue != NULL) + goto LABEL_POP; + + { + if ((rv = pthread_mutex_lock(&(sockt->mutex))) != 0) + err_exit(rv, "shm_recvfrom : pthread_mutex_lock"); - if (sockt->queue == NULL) { + if (sockt->key == 0) { sockt->key = hashtable_alloc_key(hashtable); } @@ -222,10 +231,14 @@ logger->error("%s. key = %d", bus_strerror(EBUS_KEY_INUSED), sockt->key); return EBUS_KEY_INUSED; } + + + if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0) + err_exit(rv, "shm_recvfrom : pthread_mutex_unlock"); + } - if ((rv = pthread_mutex_unlock(&(sockt->mutex))) != 0) - err_exit(rv, "shm_recvfrom : pthread_mutex_unlock"); +LABEL_POP: shm_packet_t src; @@ -397,5 +410,6 @@ const int send_size, const int send_key, void **recv_buf, int *recv_size, const struct timespec *timeout, int flags) { - return _shm_sendandrecv_thread_local(socket, send_buf, send_size, send_key,recv_buf, recv_size, timeout, flags); + struct timespec tm = {10, 0}; + return _shm_sendandrecv_thread_local(socket, send_buf, send_size, send_key,recv_buf, recv_size, &tm, flags); } -- Gitblit v1.8.0