Skip to content

Commit 627debc

Browse files
authored
Add flag to control Winsocket initialization (#3060)
When WAMR is embedded to other application, the lifecycle of the socket might conflict with other usecases. E.g. if WAMR is deinitialized before any other use of sockets, the application goes into the invalid state. The new flag allows host application to take control over the socket initialization.
1 parent 1977ad2 commit 627debc

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

core/config.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@
231231
#define WASM_ENABLE_LOG 1
232232
#endif
233233

234+
/* When this flag is set, WAMR will not automatically
235+
* initialize sockets on Windows platforms. The host
236+
* application is responsible for calling WSAStartup()
237+
* before executing WAMR code that uses sockets, and
238+
* calling WSACleanup() after.
239+
* This flag passes control of socket initialization from
240+
* WAMR to the host application. */
241+
#ifndef WASM_ENABLE_HOST_SOCKET_INIT
242+
#define WASM_ENABLE_HOST_SOCKET_INIT 0
243+
#endif
244+
234245
#ifndef WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS
235246
#if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_X86_64) \
236247
|| defined(BUILD_TARGET_AARCH64)

core/shared/platform/windows/win_socket.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static bool is_winsock_inited = false;
3232
int
3333
init_winsock()
3434
{
35+
#if WASM_ENABLE_HOST_SOCKET_INIT == 0
3536
WSADATA wsaData;
3637

3738
if (!is_winsock_inited) {
@@ -42,16 +43,19 @@ init_winsock()
4243

4344
is_winsock_inited = true;
4445
}
46+
#endif
4547

4648
return BHT_OK;
4749
}
4850

4951
void
5052
deinit_winsock()
5153
{
54+
#if WASM_ENABLE_HOST_SOCKET_INIT == 0
5255
if (is_winsock_inited) {
5356
WSACleanup();
5457
}
58+
#endif
5559
}
5660

5761
int

0 commit comments

Comments
 (0)