Linked-list-based mail packets
Some checks failed
Build documentation / build-and-deploy (push) Has been cancelled

This commit is contained in:
2026-03-17 19:21:52 +01:00
parent c69ee1b745
commit 9836fab810
3 changed files with 26 additions and 19 deletions

View File

@@ -1,6 +1,5 @@
#include <libk/list.h>
#include <libk/minmax.h>
#include <libk/ringbuffer.h>
#include <libk/std.h>
#include <libk/string.h>
#include <mm/malloc.h>
@@ -18,8 +17,6 @@ void proc_cleanup_resource_mail (struct proc_resource* resource, struct reschedu
spin_lock (&mail->resource->lock, &fr);
ringbuffer_fini (&mail->ringbuffer);
spin_lock (&mail->send_sq.lock, &fssq);
while (mail->send_sq.proc_list != NULL) {
@@ -47,7 +44,7 @@ void proc_mail_send (struct proc* proc, struct proc_mail* mail, struct reschedul
spin_lock (&mail->resource->lock, &fr);
/* mail full */
if (mail->ringbuffer.count == mail->ringbuffer.capacity) {
if (mail->packets_count == PROC_MAIL_MAX) {
proc_sq_suspend (proc, &mail->send_sq, &mail->resource->lock, fr, rctx);
return;
}
@@ -86,8 +83,19 @@ void proc_mail_send (struct proc* proc, struct proc_mail* mail, struct reschedul
void* mesg = malloc (data_size);
if (mesg != NULL) {
memcpy (mesg, data, data_size);
struct mail_packet packet = {.packet_buffer = mesg, .packet_size = data_size};
ringbuffer_push (struct mail_packet, &mail->ringbuffer, packet);
struct mail_packet* packet = malloc (sizeof (*packet));
if (packet == NULL) {
free (mesg);
} else {
memset (packet, 0, sizeof (*packet));
packet->packet_buffer = mesg;
packet->packet_size = data_size;
list_append (mail->packets, &packet->packets_link);
mail->packets_count++;
}
}
spin_unlock (&mail->resource->lock, fr);
@@ -105,11 +113,14 @@ void proc_mail_receive (struct proc* proc, struct proc_mail* mail, struct resche
spin_lock (&mail->resource->lock, &fr);
/* consume mesg if available */
if (mail->ringbuffer.count > 0) {
struct mail_packet packet;
ringbuffer_pop (struct mail_packet, &mail->ringbuffer, &packet);
memcpy (recv_buffer, packet.packet_buffer, min (recv_size, packet.packet_size));
free (packet.packet_buffer);
if (mail->packets_count > 0) {
struct mail_packet* packet = list_entry (mail->packets, struct mail_packet, packets_link);
list_remove (mail->packets, &packet->packets_link);
mail->packets_count--;
memcpy (recv_buffer, packet->packet_buffer, min (recv_size, packet->packet_size));
free (packet->packet_buffer);
free (packet);
/* check for suspended sender */
spin_lock (&mail->send_sq.lock, &fssq);

View File

@@ -16,6 +16,7 @@ struct reschedule_ctx;
struct mail_packet {
void* packet_buffer;
size_t packet_size;
struct list_node_link packets_link;
};
struct proc_mail {
@@ -23,7 +24,9 @@ struct proc_mail {
struct proc_suspension_q send_sq;
struct proc_suspension_q recv_sq;
struct ringbuffer ringbuffer;
struct list_node_link* packets;
size_t packets_count;
};
void proc_cleanup_resource_mail (struct proc_resource* resource, struct reschedule_ctx* rctx);

View File

@@ -88,13 +88,6 @@ struct proc_resource* proc_create_resource_mail (struct procgroup* procgroup) {
resource->u.mail.resource = resource;
resource->type = PR_MAIL;
if (!ringbuffer_init (&resource->u.mail.ringbuffer, PROC_MAIL_MAX, sizeof (struct mail_packet))) {
id_free (&procgroup->rid_alloc, resource->rid);
free (resource);
spin_unlock (&procgroup->lock, fpg);
return NULL;
}
rbtree_insert (struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link,
resource_tree_link, rid);