-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathasync.c
More file actions
69 lines (58 loc) · 1.69 KB
/
Copy pathasync.c
File metadata and controls
69 lines (58 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <resea/async.h>
#include <resea/ipc.h>
#include <resea/malloc.h>
#include <string.h>
#define NUM_BUCKETS 32
/// A hash table of async message queues.
static list_t *queues[NUM_BUCKETS];
static list_t *get_queue(task_t dst) {
unsigned index = dst % NUM_BUCKETS;
list_t *q = queues[index];
if (!q) {
q = malloc(sizeof(*q));
list_init(q);
queues[index] = q;
}
return q;
}
error_t async_send(task_t dst, struct message *m) {
list_t *q = get_queue(dst);
struct async_message *am = malloc(sizeof(*am));
am->dst = dst;
memcpy(&am->m, m, sizeof(am->m));
list_nullify(&am->next);
list_push_back(q, &am->next);
// Notify the destination task that a new async message is available.
return ipc_notify(dst, NOTIFY_ASYNC);
}
error_t async_recv(task_t src, struct message *m) {
m->type = ASYNC_MSG;
return ipc_call(src, m);
}
error_t async_reply(task_t dst) {
bool sent = false;
LIST_FOR_EACH (am, get_queue(dst), struct async_message, next) {
if (am->dst == dst) {
if (sent) {
// Notify that we have more messages for `dst`.
ipc_notify(dst, NOTIFY_ASYNC);
} else {
ipc_reply(dst, &am->m);
list_remove(&am->next);
free(am);
sent = true;
}
}
}
// Return ER_NOT_FOUND if there're no messages asynchronously sent to `dst`
// in the queue.
return (sent) ? OK : ERR_NOT_FOUND;
}
bool async_is_empty(task_t dst) {
LIST_FOR_EACH (am, get_queue(dst), struct async_message, next) {
if (am->dst == dst) {
return false;
}
}
return true;
}