From: Hwankyu Jhun Date: Thu, 16 Sep 2021 00:19:52 +0000 (+0900) Subject: Fix locking the mutex of the Port class X-Git-Tag: submit/tizen_6.0/20210916.024850~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fc1984e2cb45cb6b125c8844899d16192492097d;p=platform%2Fcore%2Fappfw%2Frpc-port.git Fix locking the mutex of the Port class To reduce the interval of locking the mutex, the mutex lock in the Read() method ie separated using braces. And, the mutex lock is added to the Disconnect() method. Change-Id: I60657385bc73cb988de0f53433bb1d22a21dad74 Signed-off-by: Hwankyu Jhun --- diff --git a/src/port-internal.cc b/src/port-internal.cc index ba1d4a0..b48a37e 100644 --- a/src/port-internal.cc +++ b/src/port-internal.cc @@ -107,17 +107,25 @@ int Port::Read(void* buf, unsigned int size) { int bytes_read = 0; char* buffer = static_cast(buf); int max_timeout = MAX_CNT * MAX_SLEEP; /* 10 sec */ - std::lock_guard lock(mutex_); + int fd; - if (fd_ < 0 || fd_ >= sysconf(_SC_OPEN_MAX)) { - _E("Invalid fd(%d)", fd_); - return RPC_PORT_ERROR_IO_ERROR; + { + std::lock_guard lock(mutex_); + fd = fd_; + if (fd < 0 || fd >= sysconf(_SC_OPEN_MAX)) { + _E("Invalid fd(%d)", fd); + return RPC_PORT_ERROR_IO_ERROR; + } } while (left) { - nb = read(fd_, buffer, left); + { + std::lock_guard lock(mutex_); + nb = read(fd, buffer, left); + } + if (nb == 0) { - _E("read_socket: ...read EOF, socket closed %d: nb %zd\n", fd_, nb); + _E("read_socket: ...read EOF, socket closed %d: nb %zd\n", fd, nb); return RPC_PORT_ERROR_IO_ERROR; } else if (nb == -1) { if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { @@ -125,7 +133,7 @@ int Port::Read(void* buf, unsigned int size) { nanosleep(&TRY_SLEEP_TIME, 0); max_timeout -= (TRY_SLEEP_TIME.tv_nsec / (BASE_SLEEP)); if (max_timeout <= 0) { - _E("read_socket: ...timed out fd %d: errno %d", fd_, errno); + _E("read_socket: ...timed out fd %d: errno %d", fd, errno); return RPC_PORT_ERROR_IO_ERROR; } TRY_SLEEP_TIME.tv_nsec *= 2; @@ -134,7 +142,7 @@ int Port::Read(void* buf, unsigned int size) { continue; } - _E("read_socket: ...error fd %d: errno %d\n", fd_, errno); + _E("read_socket: ...error fd %d: errno %d\n", fd, errno); return RPC_PORT_ERROR_IO_ERROR; }