mem: multi-clients support for SharedMemoryServer

Record the client session with a map instead of a single unique_ptr so
our server can interact with multiple clients at once.

This will also avoid a race condition case where the client thought it
has closed previous connection and is trying to a new one while the
server hasn't clean up the previous entry and raise a fatal error.

Change-Id: Id08154fc4b54d2611629875b3f4e0d66c0e2ed92
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/61049
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Yu-hsin Wang <yuhsingw@google.com>
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
This commit is contained in:
Jui-Min Lee
2022-07-06 15:49:45 +08:00
committed by Jui-min Lee
parent f5d15871f3
commit b6dcae31ee
2 changed files with 6 additions and 6 deletions

View File

@@ -136,12 +136,10 @@ SharedMemoryServer::ListenSocketEvent::process(int revents)
int cli_fd = ListenSocket::acceptCloexec(pfd.fd, nullptr, nullptr);
panic_if(cli_fd < 0, "%s: accept failed: %s", name().c_str(),
strerror(errno));
panic_if(shmServer->clientSocketEvent.get(),
"%s: cannot serve two clients at once", name().c_str());
inform("%s: accept new connection %d", name().c_str(), cli_fd);
shmServer->clientSocketEvent.reset(
shmServer->clientSocketEvents[cli_fd].reset(
new ClientSocketEvent(cli_fd, shmServer));
pollQueue.schedule(shmServer->clientSocketEvent.get());
pollQueue.schedule(shmServer->clientSocketEvents[cli_fd].get());
}
void
@@ -241,7 +239,7 @@ SharedMemoryServer::ClientSocketEvent::process(int revents)
// somehow broken. We'll just close the connection and move on.
inform("%s: closing connection", name().c_str());
close(pfd.fd);
shmServer->clientSocketEvent.reset();
shmServer->clientSocketEvents.erase(pfd.fd);
}
} // namespace memory

View File

@@ -30,6 +30,7 @@
#include <memory>
#include <string>
#include <unordered_map>
#include "base/pollevent.hh"
#include "params/SharedMemoryServer.hh"
@@ -86,7 +87,8 @@ class SharedMemoryServer : public SimObject
int serverFd;
std::unique_ptr<ListenSocketEvent> listenSocketEvent;
std::unique_ptr<ClientSocketEvent> clientSocketEvent;
std::unordered_map<int, std::unique_ptr<ClientSocketEvent>>
clientSocketEvents;
};
} // namespace memory