Skip to content

Commit 91901ae

Browse files
committed
add support for emscripten using upoll
1 parent ee7925a commit 91901ae

File tree

6 files changed

+942
-0
lines changed

6 files changed

+942
-0
lines changed

CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,26 @@ 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+
)
129136

137+
file(GLOB AWS_IO_OS_SRC
138+
"source/emscripten/*.c"
139+
"source/linux/*.c"
140+
"source/posix/*.c"
141+
)
142+
143+
set(EPOLL_SHIM_INCLUDE "source/emscripten")
144+
145+
set(PLATFORM_LIBS "")
146+
147+
set(EVENT_LOOP_DEFINE "EPOLL")
148+
set(USE_S2N ON)
130149
endif()
131150

132151
if (BYO_CRYPTO)
@@ -215,6 +234,7 @@ endif()
215234
target_include_directories(${PROJECT_NAME} PUBLIC
216235
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
217236
$<INSTALL_INTERFACE:include>)
237+
target_include_directories(${PROJECT_NAME} PRIVATE ${EPOLL_SHIM_INCLUDE})
218238

219239
aws_use_package(aws-c-common)
220240
aws_use_package(aws-c-cal)

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/sys/epoll.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 OPEN_MAX
24+
#define OPEN_MAX 256
25+
#endif
26+
27+
#ifndef __EPOLL_PACKED
28+
# define __EPOLL_PACKED __attribute__ ((__packed__))
29+
#endif
30+
31+
#define EPOLLIN 0x01
32+
#define EPOLLOUT 0x02
33+
#define EPOLLERR 0x04
34+
#define EPOLLET 0x08
35+
#define EPOLLHUP 0x010
36+
37+
/* Valid opcodes ( "op" parameter ) to issue to epoll_ctl(). */
38+
#define EPOLL_CTL_ADD 1 /* Add a file descriptor to the interface. */
39+
#define EPOLL_CTL_DEL 2 /* Remove a file descriptor from the interface. */
40+
#define EPOLL_CTL_MOD 3 /* Change file descriptor epoll_event structure. */
41+
42+
typedef union epoll_data
43+
{
44+
void *ptr;
45+
int fd;
46+
uint32_t u32;
47+
uint64_t u64;
48+
} epoll_data_t;
49+
50+
struct epoll_event
51+
{
52+
uint32_t events; /* Epoll events */
53+
epoll_data_t data; /* User data variable */
54+
};
55+
56+
#ifdef __cplusplus
57+
extern "C" {
58+
#endif
59+
60+
int
61+
epoll_create (int size);
62+
63+
int
64+
epoll_ctl (int epfd, int op, int fd, struct epoll_event *event);
65+
66+
int
67+
epoll_wait (int epfd, struct epoll_event *events, int maxevents, int timeout);
68+
69+
#ifdef __cplusplus
70+
}
71+
#endif
72+
73+
#endif /* sys/epoll.h */

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)