-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathumkaio.c
198 lines (167 loc) · 4.47 KB
/
umkaio.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
SPDX-License-Identifier: GPL-2.0-or-later
UMKa - User-Mode KolibriOS developer tools
io - input/output platform specific code
Copyright (C) 2023 Ivan Baravy <[email protected]>
*/
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include "umka.h"
#include "umkaio.h"
#define IOT_QUEUE_DEPTH 1
enum {
IOT_CMD_STATUS_EMPTY,
IOT_CMD_STATUS_READY,
IOT_CMD_STATUS_DONE,
};
enum {
IOT_CMD_READ,
IOT_CMD_WRITE,
};
struct iot_cmd_read_arg {
int fd;
void *buf;
size_t count;
};
struct iot_cmd_read_ret {
ssize_t val;
};
union iot_cmd_read {
struct iot_cmd_read_arg arg;
struct iot_cmd_read_ret ret;
};
struct iot_cmd_write_arg {
int fd;
void *buf;
size_t count;
};
struct iot_cmd_write_ret {
ssize_t val;
};
union iot_cmd_write {
struct iot_cmd_write_arg arg;
struct iot_cmd_write_ret ret;
};
struct iot_cmd {
pthread_cond_t iot_cond;
pthread_mutex_t iot_mutex;
pthread_mutex_t mutex;
int type;
atomic_int status;
union {
union iot_cmd_read read;
union iot_cmd_write write;
};
};
struct iot_cmd iot_cmd_buf[IOT_QUEUE_DEPTH];
static void *
thread_io(void *arg) {
(void)arg;
for (size_t i = 0; i < IOT_QUEUE_DEPTH; i++) {
iot_cmd_buf[i].status = IOT_CMD_STATUS_EMPTY;
iot_cmd_buf[i].type = 0;
pthread_cond_init(&iot_cmd_buf[i].iot_cond, NULL);
pthread_mutex_init(&iot_cmd_buf[i].iot_mutex, NULL);
pthread_mutex_lock(&iot_cmd_buf[i].iot_mutex);
pthread_mutex_init(&iot_cmd_buf[i].mutex, NULL);
}
struct iot_cmd *cmd = iot_cmd_buf;
ssize_t ret;
while (1) {
pthread_cond_wait(&cmd->iot_cond, &cmd->iot_mutex);
// status must be ready
switch (cmd->type) {
case IOT_CMD_READ:
ret = read(cmd->read.arg.fd, cmd->read.arg.buf, cmd->read.arg.count);
cmd->read.ret.val = ret;
break;
case IOT_CMD_WRITE:
cmd->read.ret.val = write(cmd->read.arg.fd, cmd->read.arg.buf,
cmd->read.arg.count);
break;
default:
break;
}
atomic_store_explicit(&cmd->status, IOT_CMD_STATUS_DONE, memory_order_release);
}
return NULL;
}
static uint32_t
io_async_submit_wait_test(void) {
// appdata_t *app;
// __asm__ __volatile__ ("":"=b"(app)::);
// struct io_uring_queue *q = app->wait_param;
int done = pthread_mutex_trylock(&iot_cmd_buf[0].mutex);
return done;
}
static uint32_t
io_async_complete_wait_test(void) {
// appdata_t *app;
// __asm__ __volatile__ ("":"=b"(app)::);
// struct io_uring_queue *q = app->wait_param;
int status = atomic_load_explicit(&iot_cmd_buf[0].status, memory_order_acquire);
return status == IOT_CMD_STATUS_DONE;
}
ssize_t
io_async_read(int fd, void *buf, size_t count, void *arg) {
(void)arg;
kos_wait_events(io_async_submit_wait_test, NULL);
// status must be empty
struct iot_cmd *cmd = iot_cmd_buf;
cmd->type = IOT_CMD_READ;
cmd->read.arg.fd = fd;
cmd->read.arg.buf = buf;
cmd->read.arg.count = count;
atomic_store_explicit(&cmd->status, IOT_CMD_STATUS_READY, memory_order_release);
pthread_cond_signal(&cmd->iot_cond);
kos_wait_events(io_async_complete_wait_test, NULL);
ssize_t res = cmd->read.ret.val;
atomic_store_explicit(&cmd->status, IOT_CMD_STATUS_EMPTY, memory_order_release);
pthread_mutex_unlock(&cmd->mutex);
return res;
}
ssize_t
io_async_write(int fd, const void *buf, size_t count, void *arg) {
(void)fd;
(void)buf;
(void)count;
(void)arg;
return -1;
}
struct umka_io *
io_init(atomic_int *running) {
struct umka_io *io = malloc(sizeof(struct umka_io));
io->running = running;
if (running) {
pthread_create(&io->iot, NULL, thread_io, NULL);
}
return io;
}
void
io_close(struct umka_io *io) {
free(io);
}
ssize_t
io_read(int fd, void *buf, size_t count, const struct umka_io *io) {
ssize_t res;
if (*io->running < UMKA_RUNNING_YES) {
res = read(fd, buf, count);
} else {
res = io_async_read(fd, buf, count, NULL);
}
return res;
}
ssize_t
io_write(int fd, const void *buf, size_t count, const struct umka_io *io) {
ssize_t res;
if (*io->running < UMKA_RUNNING_YES) {
res = write(fd, buf, count);
} else {
res = io_async_write(fd, buf, count, NULL);
}
return res;
}