[mammoth] Run EndpointServer in its own thread.

This commit is contained in:
Drew Galbraith 2023-08-01 16:08:34 -07:00
parent caccb08e16
commit c70b5b0753
4 changed files with 25 additions and 15 deletions

View File

@ -7,6 +7,7 @@
#include "mammoth/endpoint_client.h"
#include "mammoth/request_context.h"
#include "mammoth/response_context.h"
#include "mammoth/thread.h"
class EndpointServer {
public:
@ -15,13 +16,8 @@ class EndpointServer {
EndpointServer& operator=(const EndpointServer&) = delete;
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> CreateClient();
// FIXME: Release Cap here.
z_cap_t GetCap() { return endpoint_cap_; }
glcr::ErrorCode Receive(uint64_t* num_bytes, void* data,
z_cap_t* reply_port_cap);
glcr::ErrorCode RunServer();
Thread RunServer();
virtual glcr::ErrorCode HandleRequest(RequestContext& request,
ResponseContext& response) = 0;
@ -34,4 +30,7 @@ class EndpointServer {
static const uint64_t kBufferSize = 1024;
uint8_t recieve_buffer_[kBufferSize];
friend void EndpointServerThreadBootstrap(void* endpoint_server);
void ServerThread();
};

View File

@ -2,6 +2,11 @@
#include "mammoth/debug.h"
// Declared as friend in EndpointServer.
void EndpointServerThreadBootstrap(void* endpoint_server) {
reinterpret_cast<EndpointServer*>(endpoint_server)->ServerThread();
}
glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> EndpointServer::CreateClient() {
uint64_t client_cap;
// FIXME: Restrict permissions to send-only here.
@ -9,21 +14,25 @@ glcr::ErrorOr<glcr::UniquePtr<EndpointClient>> EndpointServer::CreateClient() {
return EndpointClient::AdoptEndpoint(client_cap);
}
glcr::ErrorCode EndpointServer::Receive(uint64_t* num_bytes, void* data,
z_cap_t* reply_port_cap) {
return ZEndpointRecv(endpoint_cap_, num_bytes, data, reply_port_cap);
Thread EndpointServer::RunServer() {
return Thread(EndpointServerThreadBootstrap, this);
}
glcr::ErrorCode EndpointServer::RunServer() {
void EndpointServer::ServerThread() {
while (true) {
uint64_t message_size = kBufferSize;
uint64_t reply_port_cap = 0;
RET_ERR(Receive(&message_size, recieve_buffer_, &reply_port_cap));
glcr::ErrorCode err = ZEndpointRecv(endpoint_cap_, &message_size,
recieve_buffer_, &reply_port_cap);
if (err != glcr::OK) {
dbgln("Error in receive: %x", err);
continue;
}
RequestContext request(recieve_buffer_, message_size);
ResponseContext response(reply_port_cap);
// FIXME: Consider pumping these errors into the response as well.
RET_ERR(HandleRequest(request, response));
check(HandleRequest(request, response));
if (!response.HasWritten()) {
dbgln("Returning without having written a response. Req type %x",
request.request_id());

View File

@ -26,7 +26,8 @@ uint64_t main(uint64_t init_port_cap) {
server->CreateClient());
check(stub.Register("denali", *client));
RET_ERR(server->RunServer());
// FIXME: Add thread join.
Thread server_thread = server->RunServer();
check(server_thread.Join());
return 0;
}

View File

@ -14,6 +14,7 @@ uint64_t main(uint64_t port_cap) {
ASSIGN_OR_RETURN(auto server, YellowstoneServer::Create());
Thread registration_thread = server->RunRegistration();
Thread server_thread = server->RunServer();
uint64_t vaddr;
check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootDenaliVmmoCap, &vaddr));
@ -21,7 +22,7 @@ uint64_t main(uint64_t port_cap) {
server->CreateClient());
check(SpawnProcessFromElfRegion(vaddr, glcr::Move(client)));
check(server->RunServer());
check(server_thread.Join());
check(registration_thread.Join());
dbgln("Yellowstone Finished Successfully.");
return 0;