Skip to content

Commit 56f6f24

Browse files
committed
add support for emscripten using upoll
1 parent ee7925a commit 56f6f24

File tree

6 files changed

+930
-0
lines changed

6 files changed

+930
-0
lines changed

CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,24 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "NetB
126126

127127
list(APPEND EVENT_LOOP_DEFINES "KQUEUE")
128128
set(USE_S2N ON)
129+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
130+
option(USE_VSOCK
131+
"Build in support for VSOCK sockets"
132+
OFF)
133+
134+
file(GLOB AWS_IO_OS_HEADERS
135+
"source/emscripten"
136+
)
129137

138+
file(GLOB AWS_IO_OS_SRC
139+
"source/emscripten/*.c"
140+
"source/linux/*.c"
141+
"source/posix/*.c"
142+
)
143+
set(PLATFORM_LIBS "")
144+
145+
set(EVENT_LOOP_DEFINE "EPOLL")
146+
set(USE_S2N ON)
130147
endif()
131148

132149
if (BYO_CRYPTO)

include/aws/io/private/epoll.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* epoll.h
2+
Copyright (c) fd0, All rights reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 3.0 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library.*/
16+
17+
#ifndef _SYS_EPOLL_H
18+
#define _SYS_EPOLL_H 1
19+
20+
#include <stdint.h>
21+
#include <sys/types.h>
22+
23+
#ifndef __EPOLL_PACKED
24+
# define __EPOLL_PACKED __attribute__ ((__packed__))
25+
#endif
26+
27+
#define EPOLLIN 0x01
28+
#define EPOLLOUT 0x02
29+
#define EPOLLERR 0x04
30+
#define EPOLLET 0x08
31+
32+
/* Valid opcodes ( "op" parameter ) to issue to epoll_ctl(). */
33+
#define EPOLL_CTL_ADD 1 /* Add a file descriptor to the interface. */
34+
#define EPOLL_CTL_DEL 2 /* Remove a file descriptor from the interface. */
35+
#define EPOLL_CTL_MOD 3 /* Change file descriptor epoll_event structure. */
36+
37+
typedef union epoll_data
38+
{
39+
void *ptr;
40+
int fd;
41+
uint32_t u32;
42+
uint64_t u64;
43+
} epoll_data_t;
44+
45+
struct epoll_event
46+
{
47+
uint32_t events; /* Epoll events */
48+
epoll_data_t data; /* User data variable */
49+
};
50+
51+
__BEGIN_DECLS
52+
53+
int
54+
epoll_create (int size);
55+
56+
int
57+
epoll_ctl (int epfd, int op, int fd, struct epoll_event *event);
58+
59+
int
60+
epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout);
61+
62+
__END_DECLS
63+
64+
#endif /* sys/epoll.h */

source/emscripten/epoll.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <errno.h>
2+
#include <limits.h>
3+
#include <up.h>
4+
5+
#include <sys/epoll.h>
6+
7+
static upoll_t* ups[OPEN_MAX];
8+
static int index = 1;
9+
10+
int
11+
epoll_create(int size)
12+
{
13+
if (index >= OPEN_MAX) {
14+
errno = ENFILE;
15+
return -1;
16+
}
17+
ups[index++] = upoll_create(size);
18+
return index - 1;
19+
}
20+
21+
int
22+
epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
23+
{
24+
upoll_t* upq = ups[epfd];
25+
upoll_event_t* uevent = (upoll_event_t*) event;
26+
return upoll_ctl(upq, op, fd, uevent);
27+
}
28+
29+
int
30+
epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
31+
{
32+
upoll_t* upq = ups[epfd];
33+
upoll_event_t* uevents = (upoll_event_t*) events;
34+
return upoll_wait(upq, uevents, maxevents, timeout);
35+
}

source/emscripten/up.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef _UP_H_
2+
#define _UP_H_
3+
4+
#include <stdlib.h>
5+
#include <stddef.h>
6+
#include <stdint.h>
7+
8+
#define UPOLL_CTL_ADD 1
9+
#define UPOLL_CTL_DEL 2
10+
#define UPOLL_CTL_MOD 3
11+
12+
#define UPOLLIN 0x01
13+
#define UPOLLOUT 0x02
14+
#define UPOLLERR 0x04
15+
#define UPOLLET 0x08
16+
17+
typedef struct upoll upoll_t;
18+
19+
typedef union upoll_data {
20+
void *ptr;
21+
intptr_t fd;
22+
uint32_t u32;
23+
uint64_t u64;
24+
} upoll_data_t;
25+
26+
typedef struct upoll_event {
27+
uint32_t events;
28+
upoll_data_t data;
29+
} upoll_event_t;
30+
31+
upoll_t* upoll_create(uint32_t size);
32+
int upoll_ctl(upoll_t* upq, int op, intptr_t fd, upoll_event_t *event);
33+
int upoll_wait(upoll_t* upq, upoll_event_t *events, int maxevents, int timeout);
34+
void upoll_destroy(upoll_t* upq);
35+
36+
intptr_t usocket(int domain, int type, int proto);
37+
intptr_t uaccept(intptr_t sock);
38+
39+
int ubind(intptr_t sock, const char* name, const char* serv);
40+
int ulisten(intptr_t sock, int backlog);
41+
int uconnect(intptr_t sock, const char* name, const char* serv);
42+
int uclose(intptr_t sock);
43+
int uread(intptr_t fd, char* buf, size_t len);
44+
int uwrite(intptr_t fd, const char* buf, size_t len);
45+
int usocketpair(intptr_t socks[2], int async);
46+
47+
#endif /* _UP_H_ */
48+

0 commit comments

Comments
 (0)