From 3a5e5ca9bce4bd4956e59e76a95f0ec207d711ff Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期一, 25 一月 2021 17:00:01 +0800 Subject: [PATCH] 解决kill -9问题 --- src/queue/lock_free_queue.h | 42 +++++++++++++++------ src/queue/array_lock_free_sem_queue.h | 7 +-- test_net_socket/test_net_mod_socket.cpp | 2 src/futex_sem.cpp | 6 ++- test_net_socket/heart_beat.sh | 6 +- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/futex_sem.cpp b/src/futex_sem.cpp index 226145f..8a0fd33 100644 --- a/src/futex_sem.cpp +++ b/src/futex_sem.cpp @@ -1,8 +1,10 @@ #include "futex_sem.h" - +#include "time_util.h" int futex(int *uaddr, int futex_op, int val, const struct timespec *timeout, int *uaddr2, int val3) { - return syscall(SYS_futex, uaddr, futex_op, val, timeout, uaddr, val3); + + const struct timespec ts = TimeUtil::trim_time(timeout); + return syscall(SYS_futex, uaddr, futex_op, val, &ts, uaddr, val3); } \ No newline at end of file diff --git a/src/queue/array_lock_free_sem_queue.h b/src/queue/array_lock_free_sem_queue.h index 5f56dda..28f4d81 100644 --- a/src/queue/array_lock_free_sem_queue.h +++ b/src/queue/array_lock_free_sem_queue.h @@ -241,8 +241,8 @@ if( (flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) return errno; else if( (flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { - const struct timespec ts = TimeUtil::trim_time(timeout); - s = futex((int *)&m_readIndex, FUTEX_WAIT, tmpIndex, &ts, NULL, 0); + + s = futex((int *)&m_readIndex, FUTEX_WAIT, tmpIndex, timeout, NULL, 0); if (s == -1 && errno != EAGAIN && errno != EINTR) { // err_exit("ArrayLockFreeSemQueue<ELEM_T, Allocator>::push futex-FUTEX_WAIT"); return errno; @@ -320,8 +320,7 @@ return errno; } else if( (flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { - const struct timespec ts = TimeUtil::trim_time(timeout); - s = futex((int *)&m_count, FUTEX_WAIT, 0, &ts, NULL, 0); + s = futex((int *)&m_count, FUTEX_WAIT, 0, timeout, NULL, 0); if (s == -1 && errno != EAGAIN && errno != EINTR) { // err_exit("ArrayLockFreeSemQueue<ELEM_T, Allocator>::push futex-FUTEX_WAIT"); // sigprocmask(SIG_SETMASK, &pre, NULL); diff --git a/src/queue/lock_free_queue.h b/src/queue/lock_free_queue.h index 723e373..9f03437 100644 --- a/src/queue/lock_free_queue.h +++ b/src/queue/lock_free_queue.h @@ -210,28 +210,37 @@ typename Allocator, template<typename T, typename AT> class Q_TYPE> int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::push(const ELEM_T &a_data, const struct timespec *timeout, int flag) { - LoggerFactory::getLogger()->debug("==================LockFreeQueue push before\n"); + // LoggerFactory::getLogger()->debug("==================LockFreeQueue push before\n"); + sigset_t mask_all, pre; + sigfillset(&mask_all); + + sigprocmask(SIG_BLOCK, &mask_all, &pre); + if ((flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { if (psem_trywait(&slots) == -1) { - return errno; + goto LABEL_FAILTURE; } } else if ((flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { if (psem_timedwait(&slots, timeout) == -1) { - return errno; + goto LABEL_FAILTURE; } } else { if (psem_wait(&slots) == -1) { - return errno; + goto LABEL_FAILTURE; } } if (m_qImpl.push(a_data)) { psem_post(&items); + sigprocmask(SIG_SETMASK, &pre, NULL); LoggerFactory::getLogger()->debug("==================LockFreeQueue push after\n"); return 0; } - return -1; + +LABEL_FAILTURE: + sigprocmask(SIG_SETMASK, &pre, NULL); + return errno; } @@ -239,30 +248,39 @@ typename Allocator, template<typename T, typename AT> class Q_TYPE> int LockFreeQueue<ELEM_T, Allocator, Q_TYPE>::pop(ELEM_T &a_data, const struct timespec *timeout, int flag) { - LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before...."); + // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before...."); + + sigset_t mask_all, pre; + sigfillset(&mask_all); + + sigprocmask(SIG_BLOCK, &mask_all, &pre); if ((flag & BUS_NOWAIT_FLAG) == BUS_NOWAIT_FLAG) { if (psem_trywait(&items) == -1) { - return errno; + goto LABEL_FAILTURE; } } else if ((flag & BUS_TIMEOUT_FLAG) == BUS_TIMEOUT_FLAG && timeout != NULL) { - LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before. flag=%d , %d\n", flag, timeout->tv_sec); + // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop before. flag=%d , %d\n", flag, timeout->tv_sec); if (psem_timedwait(&items, timeout) == -1) { - return errno; + goto LABEL_FAILTURE; } } else { if (psem_wait(&items) == -1) { - return errno; + goto LABEL_FAILTURE; } } if (m_qImpl.pop(a_data)) { psem_post(&slots); - LoggerFactory::getLogger()->debug("==================LockFreeQueue pop after\n"); + sigprocmask(SIG_SETMASK, &pre, NULL); + // LoggerFactory::getLogger()->debug("==================LockFreeQueue pop after\n"); return 0; } - return -1; + +LABEL_FAILTURE: + sigprocmask(SIG_SETMASK, &pre, NULL); + return errno; } template<typename ELEM_T, diff --git a/test_net_socket/heart_beat.sh b/test_net_socket/heart_beat.sh index 1fc28a6..c3bd4ef 100755 --- a/test_net_socket/heart_beat.sh +++ b/test_net_socket/heart_beat.sh @@ -1,7 +1,7 @@ #! /bin/bash PROCESSES=4 -function clean() { +function close() { ipcrm -a ps -ef | grep "heart_beat" | awk '{print $2}' | xargs -i kill -9 {} @@ -44,8 +44,8 @@ ;; - "clean") - clean + "close") + close ;; "") diff --git a/test_net_socket/test_net_mod_socket.cpp b/test_net_socket/test_net_mod_socket.cpp index 0b7c3cc..a1a47ec 100644 --- a/test_net_socket/test_net_mod_socket.cpp +++ b/test_net_socket/test_net_mod_socket.cpp @@ -269,7 +269,7 @@ fprintf(fp, "requst:%s\n", sendbuf); // n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size); n = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size, 1000); - printf("send %d nodes\n", n); + printf("%d: send %d nodes\n", i, n); for(j=0; j < recv_arr_size; j++) { fprintf(fp, "reply: host:%s, port: %d, key:%d, content: %s\n", recv_arr[j].host, -- Gitblit v1.8.0