Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure thread safety for Socket's deconstruction #205

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/Network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,17 @@
return toSockException(error);
}

Socket::Ptr Socket::createSocket(const EventPoller::Ptr &poller, bool enable_mutex) {
return std::make_shared<Socket>(poller, enable_mutex);
Socket::Ptr Socket::createSocket(const EventPoller::Ptr &poller_in, bool enable_mutex) {
auto poller = poller_in ? poller_in : EventPollerPool::Instance().getPoller();
return Socket::Ptr(new Socket(poller, enable_mutex), [poller](Socket *ptr) { poller->async([ptr]() { delete ptr; }); });
}

Socket::Socket(const EventPoller::Ptr &poller, bool enable_mutex)
: _mtx_sock_fd(enable_mutex)
Socket::Socket(EventPoller::Ptr poller, bool enable_mutex)
: _poller(std::move(poller))
, _mtx_sock_fd(enable_mutex)
, _mtx_event(enable_mutex)
, _mtx_send_buf_waiting(enable_mutex)
, _mtx_send_buf_sending(enable_mutex) {

_poller = poller;
if (!_poller) {
_poller = EventPollerPool::Instance().getPoller();
}
setOnRead(nullptr);
setOnErr(nullptr);
setOnAccept(nullptr);
Expand Down Expand Up @@ -274,7 +271,7 @@

while (_enable_recv) {
do {
nread = recvfrom(sock->rawFd(), data, capacity, 0, (struct sockaddr *)&addr, &len);

Check warning on line 274 in src/Network/Socket.cpp

View workflow job for this annotation

GitHub Actions / build

'argument': conversion from 'size_t' to 'int', possible loss of data
} while (-1 == nread && UV_EINTR == get_uv_error(true));

if (nread == 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/Network/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ class Socket : public std::enable_shared_from_this<Socket>, public noncopyable,
* @param enable_mutex 是否启用互斥锁(接口是否线程安全)
*/
static Ptr createSocket(const EventPoller::Ptr &poller = nullptr, bool enable_mutex = true);
Socket(const EventPoller::Ptr &poller = nullptr, bool enable_mutex = true);
~Socket() override;

/**
Expand Down Expand Up @@ -512,6 +511,8 @@ class Socket : public std::enable_shared_from_this<Socket>, public noncopyable,
std::string getIdentifier() const override;

private:
Socket(EventPoller::Ptr poller, bool enable_mutex = true);

void setSock(SockNum::Ptr sock);
int onAccept(const SockNum::Ptr &sock, int event) noexcept;
ssize_t onRead(const SockNum::Ptr &sock, const BufferRaw::Ptr &buffer) noexcept;
Expand Down
Loading