Skip to content

Commit

Permalink
ipc: added per-socket locks
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Oct 8, 2024
1 parent 2c004a5 commit ac6e6ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/include/kernel/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <kernel/sched.h>
#include <sys/types.h>
#include <kernel/sched.h>
#include <platform/lock.h>

/* system-wide limits */
#define MAX_SOCKETS (1 << 18) // 262k
Expand Down Expand Up @@ -57,6 +58,7 @@ struct sockaddr_un {
typedef struct SocketDescriptor {
Process *process;
struct sockaddr address;
lock_t lock;
bool listener;
int globalIndex;
int type, protocol, backlogMax, backlogCount;
Expand Down
38 changes: 16 additions & 22 deletions src/ipc/sockio.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ ssize_t send(Thread *t, int sd, const void *buffer, size_t len, int flags) {

if(!p->io[sd].valid || !p->io[sd].data || (p->io[sd].type != IO_SOCKET))
return -ENOTSOCK;

socketLock();

SocketDescriptor *self = (SocketDescriptor*) p->io[sd].data;
SocketDescriptor *peer = self->peer;
if(!peer) {
socketRelease();
return -EDESTADDRREQ; // not in connection mode
}
if(!peer) return -EDESTADDRREQ; // not in connection mode

acquireLockBlocking(&peer->lock);

sa_family_t family = self->address.sa_family;

Expand All @@ -52,7 +49,7 @@ ssize_t send(Thread *t, int sd, const void *buffer, size_t len, int flags) {
peer->inboundLen = calloc(SOCKET_IO_BACKLOG, sizeof(size_t));

if(!peer->inbound || !peer->inboundLen) {
socketRelease();
releaseLock(&peer->lock);
return -ENOMEM;
}

Expand All @@ -64,15 +61,15 @@ ssize_t send(Thread *t, int sd, const void *buffer, size_t len, int flags) {
// reallocate the backlog if necessary
void **newlist = realloc(peer->inbound, peer->inboundMax * 2 * sizeof(void *));
if(!newlist) {
socketRelease();
releaseLock(&peer->lock);
return -ENOMEM;
}

peer->inbound = newlist;

size_t *newlen = realloc(peer->inboundLen, peer->inboundMax * 2 * sizeof(void *));
if(!newlen) {
socketRelease();
releaseLock(&peer->lock);
return -ENOMEM;
}

Expand All @@ -82,7 +79,7 @@ ssize_t send(Thread *t, int sd, const void *buffer, size_t len, int flags) {

void *message = malloc(len);
if(!message) {
socketRelease();
releaseLock(&peer->lock);
return -ENOBUFS;
}

Expand All @@ -92,11 +89,11 @@ ssize_t send(Thread *t, int sd, const void *buffer, size_t len, int flags) {
peer->inboundLen[peer->inboundCount] = len;
peer->inboundCount++;

socketRelease();
releaseLock(&peer->lock);
return len;
} else {
/* TODO: handle other protocols in user space */
socketRelease();
releaseLock(&peer->lock);
return -ENOTCONN;
}
}
Expand All @@ -119,17 +116,14 @@ ssize_t recv(Thread *t, int sd, void *buffer, size_t len, int flags) {
if(!p->io[sd].valid || !p->io[sd].data || (p->io[sd].type != IO_SOCKET))
return -ENOTSOCK;

socketLock();

SocketDescriptor *self = (SocketDescriptor*) p->io[sd].data;
if(!self->peer) {
socketRelease();
return -EDESTADDRREQ; // not in connection mode
}
if(!self->peer) return -EDESTADDRREQ; // not in connection mode

acquireLockBlocking(&self->lock);

sa_family_t family = self->address.sa_family;
if(!self->inboundCount || !self->inbound || !self->inboundLen) {
socketRelease();
releaseLock(&self->lock);
return -EWOULDBLOCK; // no messages available
}

Expand All @@ -139,7 +133,7 @@ ssize_t recv(Thread *t, int sd, void *buffer, size_t len, int flags) {
size_t truelen = self->inboundLen[0];

if(!message) {
socketRelease();
releaseLock(&self->lock);
return -EWOULDBLOCK;
}

Expand All @@ -157,11 +151,11 @@ ssize_t recv(Thread *t, int sd, void *buffer, size_t len, int flags) {
}
}

socketRelease();
releaseLock(&self->lock);
return truelen;
} else {
/* TODO: handle other protocols in user space */
socketRelease();
releaseLock(&self->lock);
return -ENOTCONN;
}
}

0 comments on commit ac6e6ca

Please sign in to comment.