rpmsg_socket: move rpmsg_send out of lock

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
ligd 2021-09-13 12:18:28 +08:00 committed by Xiang Xiao
parent 163109c80f
commit bb41af3935

View File

@ -260,21 +260,21 @@ static int rpmsg_socket_wakeup(FAR struct rpmsg_socket_conn_s *conn)
return ret; return ret;
} }
rpmsg_socket_lock(&conn->recvlock);
space = conn->recvpos - conn->lastpos; space = conn->recvpos - conn->lastpos;
if (space > circbuf_size(&conn->recvbuf) / 2) if (space > circbuf_size(&conn->recvbuf) / 2)
{ {
conn->lastpos = conn->recvpos;
msg.cmd = RPMSG_SOCKET_CMD_DATA; msg.cmd = RPMSG_SOCKET_CMD_DATA;
msg.pos = conn->recvpos; msg.pos = conn->recvpos;
msg.len = 0; msg.len = 0;
ret = rpmsg_send(&conn->ept, &msg, sizeof(msg)); ret = 1;
if (ret >= 0)
{
conn->lastpos = conn->recvpos;
}
} }
return ret; rpmsg_socket_unlock(&conn->recvlock);
return ret ? rpmsg_send(&conn->ept, &msg, sizeof(msg)) : 0;
} }
static inline uint32_t rpmsg_socket_get_space( static inline uint32_t rpmsg_socket_get_space(
@ -908,21 +908,21 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock,
uint32_t offset = 0; uint32_t offset = 0;
int ret = 0; int ret = 0;
rpmsg_socket_lock(&conn->sendlock);
while (written < len) while (written < len)
{ {
uint32_t block = MIN(len - written, rpmsg_socket_get_space(conn));
FAR struct rpmsg_socket_data_s *msg; FAR struct rpmsg_socket_data_s *msg;
uint32_t block_written = 0; uint32_t block_written = 0;
uint32_t ipcsize; uint32_t ipcsize;
uint32_t block;
rpmsg_socket_lock(&conn->sendlock);
block = MIN(len - written, rpmsg_socket_get_space(conn));
rpmsg_socket_unlock(&conn->sendlock);
if (block == 0) if (block == 0)
{ {
if (!_SS_ISNONBLOCK(psock->s_flags)) if (!_SS_ISNONBLOCK(psock->s_flags))
{ {
rpmsg_socket_unlock(&conn->sendlock);
ret = net_timedwait(&conn->sendsem, ret = net_timedwait(&conn->sendsem,
_SO_TIMEOUT(psock->s_sndtimeo)); _SO_TIMEOUT(psock->s_sndtimeo));
if (!conn->ept.rdev) if (!conn->ept.rdev)
@ -930,8 +930,6 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock,
ret = -ECONNRESET; ret = -ECONNRESET;
} }
rpmsg_socket_lock(&conn->sendlock);
if (ret < 0) if (ret < 0)
{ {
break; break;
@ -942,6 +940,8 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock,
ret = -EAGAIN; ret = -EAGAIN;
break; break;
} }
continue;
} }
msg = rpmsg_get_tx_payload_buffer(&conn->ept, &ipcsize, true); msg = rpmsg_get_tx_payload_buffer(&conn->ept, &ipcsize, true);
@ -951,6 +951,9 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock,
break; break;
} }
rpmsg_socket_lock(&conn->sendlock);
block = MIN(len - written, rpmsg_socket_get_space(conn));
block = MIN(block, ipcsize - sizeof(*msg)); block = MIN(block, ipcsize - sizeof(*msg));
msg->cmd = RPMSG_SOCKET_CMD_DATA; msg->cmd = RPMSG_SOCKET_CMD_DATA;
@ -974,6 +977,8 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock,
conn->lastpos = conn->recvpos; conn->lastpos = conn->recvpos;
conn->sendpos += msg->len; conn->sendpos += msg->len;
rpmsg_socket_unlock(&conn->sendlock);
ret = rpmsg_send_nocopy(&conn->ept, msg, block + sizeof(*msg)); ret = rpmsg_send_nocopy(&conn->ept, msg, block + sizeof(*msg));
if (ret < 0) if (ret < 0)
{ {
@ -983,8 +988,6 @@ static ssize_t rpmsg_socket_send_continuous(FAR struct socket *psock,
written += block; written += block;
} }
rpmsg_socket_unlock(&conn->sendlock);
return written ? written : ret; return written ? written : ret;
} }
@ -998,6 +1001,7 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock,
uint32_t total = len + sizeof(*msg) + sizeof(uint32_t); uint32_t total = len + sizeof(*msg) + sizeof(uint32_t);
uint32_t written = 0; uint32_t written = 0;
uint32_t ipcsize; uint32_t ipcsize;
uint32_t space;
char *msgpos; char *msgpos;
int ret; int ret;
@ -1006,14 +1010,17 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock,
return -EFBIG; return -EFBIG;
} }
rpmsg_socket_lock(&conn->sendlock); while (1)
while (total - sizeof(*msg) > rpmsg_socket_get_space(conn))
{ {
rpmsg_socket_lock(&conn->sendlock);
space = rpmsg_socket_get_space(conn);
rpmsg_socket_unlock(&conn->sendlock);
if (space >= total - sizeof(*msg))
break;
if (!_SS_ISNONBLOCK(psock->s_flags)) if (!_SS_ISNONBLOCK(psock->s_flags))
{ {
rpmsg_socket_unlock(&conn->sendlock);
ret = net_timedwait(&conn->sendsem, ret = net_timedwait(&conn->sendsem,
_SO_TIMEOUT(psock->s_sndtimeo)); _SO_TIMEOUT(psock->s_sndtimeo));
if (!conn->ept.rdev) if (!conn->ept.rdev)
@ -1021,32 +1028,29 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock,
ret = -ECONNRESET; ret = -ECONNRESET;
} }
rpmsg_socket_lock(&conn->sendlock);
if (ret < 0) if (ret < 0)
{ {
goto out; return ret;
} }
} }
else else
{ {
ret = -EAGAIN; return -EAGAIN;
goto out;
} }
} }
msg = rpmsg_get_tx_payload_buffer(&conn->ept, &ipcsize, true); msg = rpmsg_get_tx_payload_buffer(&conn->ept, &ipcsize, true);
if (!msg) if (!msg)
{ {
ret = -EINVAL; return -EINVAL;
goto out;
} }
if (total > ipcsize) rpmsg_socket_lock(&conn->sendlock);
{
total = ipcsize; space = rpmsg_socket_get_space(conn);
len = ipcsize - sizeof(*msg) - sizeof(uint32_t); total = MIN(total, space + sizeof(*msg));
} total = MIN(total, ipcsize);
len = total - sizeof(*msg) - sizeof(uint32_t);
/* SOCK_DGRAM need write len to buffer */ /* SOCK_DGRAM need write len to buffer */
@ -1074,9 +1078,10 @@ static ssize_t rpmsg_socket_send_single(FAR struct socket *psock,
conn->lastpos = conn->recvpos; conn->lastpos = conn->recvpos;
conn->sendpos += len + sizeof(uint32_t); conn->sendpos += len + sizeof(uint32_t);
ret = rpmsg_send_nocopy(&conn->ept, msg, total);
out:
rpmsg_socket_unlock(&conn->sendlock); rpmsg_socket_unlock(&conn->sendlock);
ret = rpmsg_send_nocopy(&conn->ept, msg, total);
return ret > 0 ? len : ret; return ret > 0 ? len : ret;
} }
@ -1180,7 +1185,6 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock,
if (ret > 0) if (ret > 0)
{ {
rpmsg_socket_wakeup(conn);
goto out; goto out;
} }
@ -1215,7 +1219,6 @@ static ssize_t rpmsg_socket_recvmsg(FAR struct socket *psock,
if (!conn->recvdata) if (!conn->recvdata)
{ {
ret = conn->recvlen; ret = conn->recvlen;
rpmsg_socket_wakeup(conn);
} }
else else
{ {
@ -1227,6 +1230,7 @@ out:
if (ret > 0) if (ret > 0)
{ {
rpmsg_socket_wakeup(conn);
rpmsg_socket_getaddr(conn, from, fromlen); rpmsg_socket_getaddr(conn, from, fromlen);
} }