LF OS
Hobby operating system for amd64 with high ambitions
Loading...
Searching...
No Matches
sd.c
Go to the documentation of this file.
1#include <sd.h>
2#include <vm.h>
3#include <mq.h>
4#include <uuid.h>
5#include <flexarray.h>
6#include <string.h>
7#include <log.h>
8#include <errno.h>
9#include <scheduler.h>
10
11struct sd_entry {
12 struct sd_entry* prev;
13 struct sd_entry* next;
14
16 size_t push_idx;
17 mq_id_t queues[(4080 - sizeof(uuid_t)
18 - sizeof(size_t)
19 - (sizeof(struct sd_entry*) * 2)
20 ) / sizeof(mq_id_t)];
21};
22
23struct sd {
25
26 struct sd_entry* tail;
27 struct sd_entry* head;
28};
29
30static struct sd sd_global_data;
31
35
37 struct sd_entry* entry = (struct sd_entry*)kernel_alloc.alloc(&kernel_alloc, (sizeof(struct sd_entry)));
38 memset(entry, 0, sizeof(struct sd_entry));
39 memcpy(&entry->uuid, uuid, sizeof(uuid_t));
40 entry->queues[entry->push_idx++] = queue;
41
42 return entry;
43}
44
46 char uuid_s[38];
47 uuid_fmt(uuid_s, sizeof(uuid_s), uuid);
48
49 logd("sd", "Registering queue %u/%u for service %s", scheduler_current_process, svc_queue, uuid_s);
50
51 uuid_key_t key = uuid_key(uuid);
53
54 if(!entry) {
55 entry = new_sd_entry(uuid, svc_queue);
56
60 }
61 else {
64 }
65
67 }
68 else if(uuid_cmp(&entry->uuid, uuid) != 0) {
69 struct sd_entry* cur = entry;
70
71 if(uuid_cmp(&entry->uuid, uuid) < 0) {
72 cur = entry->prev;
73 }
74 else {
75 while(cur->next && uuid_key(&cur->next->uuid) == key && uuid_cmp(&cur->next->uuid, uuid) > 0) {
76 cur = cur->next;
77 }
78 }
79
80 entry = new_sd_entry(uuid, svc_queue);
81
82 if(cur->prev) {
83 cur->prev->next = entry;
84 }
85
86 entry->next = cur;
87 entry->prev = cur->prev;
88 cur->prev = entry;
89
91 }
92 else {
93 for(size_t i = 0; i < entry->push_idx; ++i) {
94 if(entry->queues[i] == svc_queue) {
95 logw("sd", "Tried to register queue for service %s a second time!", uuid_s);
96 }
97 }
98
99 if(entry->push_idx >= sizeof(entry->queues) / sizeof(mq_id_t)) {
100 return ENOMEM;
101 }
102
103 entry->queues[entry->push_idx++] = svc_queue;
104 }
105
106 return 0;
107}
108
110 uuid_key_t key = uuid_key(uuid);
112
113 while(entry && uuid_cmp(&entry->uuid, uuid) != 0) {
114 entry = entry->next;
115 }
116
117 if(!entry) {
118 return -ENOENT;
119 }
120
121 unsigned success = 0;
122
123 for(size_t i = 0; i < entry->push_idx; ++i) {
124 if(mq_push(entry->queues[i], msg) == 0) {
125 ++success;
126 }
127 }
128
129 return success;
130}
allocator_t kernel_alloc
Definition vm.c:737
unsigned long uint64_t
Definition arch.h:14
signed long int64_t
Definition arch.h:13
#define ENOENT
Definition errno-defs.h:5
#define ENOMEM
Definition errno-defs.h:11
void * memcpy(void *dest, void const *source, size_t size)
Definition string.c:80
void * memset(void *dest, int c, size_t size)
Definition string.c:72
#define logd(component, fmt,...)
Definition log.h:28
#define logw(component, fmt,...)
Definition log.h:44
uint64_t mq_push(uint64_t mq, struct Message *message)
Definition mq.c:144
uint64_t mq_id_t
Definition mq.h:9
volatile pid_t scheduler_current_process
Definition scheduler.c:50
void init_sd(void)
Definition sd.c:32
struct sd_entry * tail
Definition sd.c:26
static struct sd sd_global_data
Definition sd.c:30
uuid_t uuid
Definition sd.c:15
struct sd_entry * next
Definition sd.c:13
struct sd_entry * prev
Definition sd.c:12
mq_id_t queues[(4080 - sizeof(uuid_t) - sizeof(size_t) -(sizeof(struct sd_entry *) *2))/sizeof(mq_id_t)]
Definition sd.c:20
int64_t sd_send(uuid_t *uuid, struct Message *msg)
Definition sd.c:109
struct sd_entry * head
Definition sd.c:27
struct sd_entry * entry_shortcut[256]
Definition sd.c:24
uint64_t sd_register(uuid_t *uuid, mq_id_t svc_queue)
Definition sd.c:45
size_t push_idx
Definition sd.c:16
static struct sd_entry * new_sd_entry(uuid_t *uuid, mq_id_t queue)
Definition sd.c:36
Definition sd.c:23
Definition sd.c:11
void *(* alloc)(struct allocator *alloc, size_t size)
Definition allocator.h:9
static void * entry
Definition syscalls.h:34
static bool uint64_t queue
Definition syscalls.h:143
uuid_key_t uuid_key(uuid_t *uuid)
Definition uuid.c:4
size_t uuid_fmt(char *buffer, size_t len, uuid_t *uuid)
Definition uuid.c:18
int uuid_cmp(uuid_t *a, uuid_t *b)
Definition uuid.c:14
Definition uuid.h:6