Skip to content

Commit be067fe

Browse files
committed
Implement multiprotocol transport
1 parent 54fb5f8 commit be067fe

13 files changed

+164
-205
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
LIB?=able
2-
SRCS=edge.c node.c port.c link.c wire.c task.c
3-
HDRS=edge.h node.h port.h link.h wire.h task.h able.h
2+
SRCS=edge.c node.c link.c mesg.c task.c
3+
HDRS=edge.h node.h link.h mesg.h task.h able.h
44

55
.include "config.mk"
66

able.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "edge.h"
22
#include "node.h"
3-
#include "port.h"
43
#include "link.h"
5-
#include "wire.h"
4+
#include "mesg.h"
65
#include "task.h"

link.c

+39-25
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,68 @@
22
#include "edge.h"
33
#include <pthread.h>
44
#include "node.h"
5-
#include "port.h"
65
#include "link.h"
7-
#include <stdbool.h>
86
#include <string.h>
97

108
int
11-
able_link_send_long(able_link_t *link, uint16_t size, void **data) {
9+
able_link_join(able_link_t *link, able_edge_t *edge, uintptr_t mark, void *node) {
10+
int v;
11+
v = 0;
12+
while (!atomic_compare_exchange_weak(&link->sl, &v, -1)) {
13+
if (v == 1)
14+
return -1;
15+
v = 0;
16+
}
17+
link->e = edge;
18+
link->i = mark;
19+
link->n = node;
20+
atomic_store(&link->sl, 0);
21+
return 0;
22+
}
23+
24+
int
25+
able_link_send_long(able_link_t *link, size_t size, void **data) {
1226
if (size == 0)
1327
return 1;
14-
bool z;
15-
z = 0;
16-
if (!atomic_compare_exchange_weak(&link->sl, &z, 1))
17-
return -3;
18-
if (link->p == NULL) {
28+
int v;
29+
v = 0;
30+
while (!atomic_compare_exchange_weak(&link->sl, &v, -1)) {
31+
if (v == 1)
32+
return -3;
33+
v = 0;
34+
}
35+
if (link->e == NULL) {
1936
atomic_store(&link->sl, 0);
2037
return 3;
2138
}
2239
int y;
23-
y = able_port_send_long(link->p, size, data, link->i);
24-
if (y != 0)
40+
y = able_edge_send_long(link->e, size, data);
41+
if (y != 0) {
2542
atomic_store(&link->sl, 0);
26-
return y;
43+
return y;
44+
}
45+
atomic_store(&link->sl, 1);
46+
return 0;
2747
}
2848

2949
int
30-
able_link_send_done(able_link_t *link, uint16_t size) {
31-
able_port_t *p;
32-
p = link->p;
33-
if (size == 0) {
34-
int y;
35-
y = able_port_send_done(p, 0);
36-
atomic_store(&link->sl, 0);
37-
return y;
38-
}
39-
void *n;
40-
n = link->n;
50+
able_link_send_done(able_link_t *link, size_t size) {
51+
able_edge_t *e;
52+
e = link->e;
4153
int y;
42-
y = able_port_send_done(p, size);
54+
y = able_edge_send_done(e, size);
4355
if (y != 0) {
4456
atomic_store(&link->sl, 0);
4557
return y;
4658
}
59+
void *n;
60+
n = link->n;
4761
atomic_store(&link->sl, 0);
48-
return able_link_node_post_shim(n, &p->e);
62+
return able_link_node_post_shim(n, e);
4963
}
5064

5165
int
52-
able_link_send(able_link_t *link, const void *data, uint16_t size) {
66+
able_link_send(able_link_t *link, const void *data, size_t size) {
5367
void *mb;
5468
int y;
5569
y = able_link_send_long(link, size, &mb);

link.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
typedef struct {
2-
atomic_bool sl;
3-
able_port_t *p;
4-
uint32_t i;
2+
atomic_int sl;
3+
able_edge_t *e;
4+
uintptr_t i;
55
void *n;
66
} able_link_t;
77

88
int
9-
able_link_send_long(able_link_t *link, uint16_t size, void **data);
9+
able_link_join(able_link_t *link, able_edge_t *edge, uintptr_t mark, void *node);
1010

1111
int
12-
able_link_send_done(able_link_t *link, uint16_t size);
12+
able_link_send_long(able_link_t *link, size_t size, void **data);
1313

1414
int
15-
able_link_send(able_link_t *link, const void *data, uint16_t size);
15+
able_link_send_done(able_link_t *link, size_t size);
16+
17+
int
18+
able_link_send(able_link_t *link, const void *data, size_t size);
1619

1720
int
1821
able_link_node_post_shim(void *node, const able_edge_t *edge);

mesg.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <stdatomic.h>
2+
#include "edge.h"
3+
#include <pthread.h>
4+
#include "node.h"
5+
#include "link.h"
6+
#include "mesg.h"
7+
#include <string.h>
8+
9+
int
10+
able_mesg_link_send_long(able_link_t *link, uint16_t size, void **data) {
11+
size_t sc;
12+
sc = ABLE_MESG_SIZE(able_mesg_t, size);
13+
if (sc > UINT16_MAX)
14+
return 4;
15+
able_mesg_t *m;
16+
int y;
17+
y = able_link_send_long(link, sc, (void **)&m);
18+
if (y != 0)
19+
return y;
20+
m->i = link->i;
21+
*data = m->b;
22+
return 0;
23+
}
24+
25+
int
26+
able_mesg_link_send_done(able_link_t *link, uint16_t size) {
27+
size_t sc;
28+
sc = ABLE_MESG_SIZE(able_mesg_t, size);
29+
if (sc > UINT16_MAX)
30+
return 4;
31+
able_mesg_t *m;
32+
m = (able_mesg_t *)link->e->s;
33+
m->sc = sc;
34+
m->bc = size;
35+
return able_link_send_done(link, sc);
36+
}
37+
38+
int
39+
able_mesg_link_send(able_link_t *link, const void *data, uint16_t size) {
40+
void *mb;
41+
int y;
42+
y = able_mesg_link_send_long(link, size, &mb);
43+
if (y != 0)
44+
return y;
45+
memcpy(mb, data, size);
46+
return able_mesg_link_send_done(link, size);
47+
}

mesg.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define ABLE_MESG_SIZE(H, B) \
2+
(((sizeof(H) + (B)) + ((sizeof(uint64_t)) - 1)) & -(sizeof(uint64_t)))
3+
4+
typedef struct {
5+
uint16_t sc;
6+
uint16_t bc;
7+
uint32_t i;
8+
uint8_t b[0];
9+
} able_mesg_t;
10+
11+
int
12+
able_mesg_link_send_long(able_link_t *link, uint16_t size, void **data);
13+
14+
int
15+
able_mesg_link_send_done(able_link_t *link, uint16_t size);
16+
17+
int
18+
able_mesg_link_send(able_link_t *link, const void *data, uint16_t size);

misc/host.c

+38-24
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ able_misc_host_exec(able_misc_host_t *host) {
2424
case 0x80: { // wait ( t p - n)
2525
if (DSU(&host->c, 2))
2626
return -6;
27-
uint32_t pn;
28-
pn = DS0;
27+
uint32_t en;
28+
en = DS0;
2929
DSD(&host->c);
30-
if (pn >= host->pc) {
30+
if (en >= host->ec) {
3131
DS0 = 4;
3232
break;
3333
}
@@ -41,23 +41,23 @@ able_misc_host_exec(able_misc_host_t *host) {
4141
ts.tv_nsec = tv % 1000000000;
4242
tp = &ts;
4343
}
44-
DS0 = able_misc_host_node_wait_shim(host->n, &host->p[pn].e, tp);
44+
DS0 = able_misc_host_node_wait_shim(host->n, &host->e[en], tp);
4545
if (DS0 == 0)
4646
return -5;
4747
break;
4848
}
4949
case 0x81: { // clip ( a # p - n)
5050
if (DSU(&host->c, 3))
5151
return -6;
52-
uint32_t pn;
53-
pn = DS0;
52+
uint32_t en;
53+
en = DS0;
5454
DSD(&host->c);
5555
uint64_t u;
5656
u = DS0;
5757
DSD(&host->c);
5858
uint64_t a;
5959
a = DS0;
60-
if (pn >= host->pc) {
60+
if (en >= host->ec) {
6161
DS0 = 2;
6262
break;
6363
}
@@ -69,35 +69,53 @@ able_misc_host_exec(able_misc_host_t *host) {
6969
DS0 = 4;
7070
break;
7171
}
72-
DS0 = able_port_clip(&host->p[pn], host->c.b + a, u);
72+
able_misc_host_buff_t *b;
73+
b = &host->b[en];
74+
if (b->rc > 0) {
75+
DS0 = 1;
76+
break;
77+
}
78+
uint8_t *s;
79+
s = host->c.b + a;
80+
int y;
81+
y = able_edge_clip(&host->e[en], s, u);
82+
if (y == 0)
83+
b->r = s;
84+
DS0 = y;
7385
break;
7486
}
7587
case 0x82: { // recv ( p - a # n)
7688
if (DSU(&host->c, 1))
7789
return -6;
7890
if (DSO(&host->c, 2))
7991
return -7;
80-
uint32_t pn;
81-
pn = DS0;
82-
if (pn >= host->pc) {
92+
uint32_t en;
93+
en = DS0;
94+
if (en >= host->ec) {
8395
DS0 = 1;
8496
break;
8597
}
86-
able_port_mesg_t *m;
87-
m = able_port_recv(&host->p[pn]);
88-
if (m != NULL) {
89-
DS0 = m->b - host->c.b;
90-
DSI(&host->c);
91-
DS0 = m->bc;
92-
DSI(&host->c);
93-
DS0 = 0;
94-
} else {
98+
able_misc_host_buff_t *b;
99+
b = &host->b[en];
100+
if (b->rc == 0)
101+
b->rc += able_edge_recv(&host->e[en]);
102+
able_mesg_t *m;
103+
if (b->rc < sizeof(able_mesg_t)) {
95104
DS0 = 0;
96105
DSI(&host->c);
97106
DS0 = 0;
98107
DSI(&host->c);
99108
DS0 = -1;
109+
break;
100110
}
111+
m = (able_mesg_t *)b->r;
112+
b->r += m->sc;
113+
b->rc -= m->sc;
114+
DS0 = m->b - host->c.b;
115+
DSI(&host->c);
116+
DS0 = m->bc;
117+
DSI(&host->c);
118+
DS0 = 0;
101119
break;
102120
}
103121
case 0x83: { // send ( a # l - n)
@@ -123,10 +141,6 @@ able_misc_host_exec(able_misc_host_t *host) {
123141
DS0 = 7;
124142
break;
125143
}
126-
if (u > UINT16_MAX - sizeof(able_port_mesg_t)) {
127-
DS0 = 8;
128-
break;
129-
}
130144
DS0 = able_misc_host_link_send_shim(host->l[ln], host->c.b + a, u);
131145
break;
132146
}

misc/host.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
typedef struct {
2+
uint8_t *r;
3+
size_t rc;
4+
} able_misc_host_buff_t;
5+
16
typedef struct {
27
void *n;
3-
able_port_t *p;
4-
uint32_t pc;
8+
able_edge_t *e;
9+
uint32_t ec;
10+
able_misc_host_buff_t *b;
11+
uint32_t bc;
512
void **l;
613
uint32_t lc;
714
able_misc_core_t c;

node.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ able_node_post(able_node_t *node, const able_edge_t *edge) {
5252
pthread_mutex_lock(&node->m);
5353
if (node->w != edge) {
5454
pthread_mutex_unlock(&node->m);
55-
return 4;
55+
return 0;
5656
}
5757
pthread_cond_signal(&node->v);
5858
pthread_mutex_unlock(&node->m);

0 commit comments

Comments
 (0)