Skip to content

Commit

Permalink
#1462 Move threading check in HTTP server to start/stop methods.
Browse files Browse the repository at this point in the history
When using the flecs_explorer_request hook from wasm with FLECS_NO_OS_API_IMPL
set, the leack of threading support prevents the http server from being
created. We dont need lock support if we don't plan to actually launch the
resulting server, so move the check that we have that support to starting/stopping
the server.
  • Loading branch information
basicer authored Dec 11, 2024
1 parent c0435e2 commit b5b285d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
17 changes: 10 additions & 7 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -23405,11 +23405,10 @@ const char* ecs_http_get_param(
ecs_http_server_t* ecs_http_server_init(
const ecs_http_server_desc_t *desc)
{
ecs_check(ecs_os_has_threading(), ECS_UNSUPPORTED,
"missing OS API implementation");

ecs_http_server_t* srv = ecs_os_calloc_t(ecs_http_server_t);
srv->lock = ecs_os_mutex_new();
if (ecs_os_has_threading()) {
srv->lock = ecs_os_mutex_new();
}
srv->sock = HTTP_SOCKET_INVALID;

srv->should_run = false;
Expand Down Expand Up @@ -23452,8 +23451,6 @@ ecs_http_server_t* ecs_http_server_init(
#endif

return srv;
error:
return NULL;
}

void ecs_http_server_fini(
Expand All @@ -23462,7 +23459,9 @@ void ecs_http_server_fini(
if (srv->should_run) {
ecs_http_server_stop(srv);
}
ecs_os_mutex_free(srv->lock);
if (ecs_os_has_threading()) {
ecs_os_mutex_free(srv->lock);
}
http_purge_request_cache(srv, true);
flecs_sparse_fini(&srv->requests);
flecs_sparse_fini(&srv->connections);
Expand All @@ -23476,6 +23475,8 @@ int ecs_http_server_start(
ecs_check(srv->initialized, ECS_INVALID_PARAMETER, NULL);
ecs_check(!srv->should_run, ECS_INVALID_PARAMETER, NULL);
ecs_check(!srv->thread, ECS_INVALID_PARAMETER, NULL);
ecs_check(ecs_os_has_threading(), ECS_UNSUPPORTED,
"missing OS API implementation");

srv->should_run = true;

Expand Down Expand Up @@ -23504,6 +23505,8 @@ void ecs_http_server_stop(
"cannot stop HTTP server: not initialized");
ecs_check(srv->should_run, ECS_INVALID_PARAMETER,
"cannot stop HTTP server: already stopped/stopping");
ecs_check(ecs_os_has_threading(), ECS_UNSUPPORTED,
"missing OS API implementation");

/* Stop server thread */
ecs_dbg("http: shutting down server thread");
Expand Down
17 changes: 10 additions & 7 deletions src/addons/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1484,11 +1484,10 @@ const char* ecs_http_get_param(
ecs_http_server_t* ecs_http_server_init(
const ecs_http_server_desc_t *desc)
{
ecs_check(ecs_os_has_threading(), ECS_UNSUPPORTED,
"missing OS API implementation");

ecs_http_server_t* srv = ecs_os_calloc_t(ecs_http_server_t);
srv->lock = ecs_os_mutex_new();
if (ecs_os_has_threading()) {
srv->lock = ecs_os_mutex_new();
}
srv->sock = HTTP_SOCKET_INVALID;

srv->should_run = false;
Expand Down Expand Up @@ -1531,8 +1530,6 @@ ecs_http_server_t* ecs_http_server_init(
#endif

return srv;
error:
return NULL;
}

void ecs_http_server_fini(
Expand All @@ -1541,7 +1538,9 @@ void ecs_http_server_fini(
if (srv->should_run) {
ecs_http_server_stop(srv);
}
ecs_os_mutex_free(srv->lock);
if (ecs_os_has_threading()) {
ecs_os_mutex_free(srv->lock);
}
http_purge_request_cache(srv, true);
flecs_sparse_fini(&srv->requests);
flecs_sparse_fini(&srv->connections);
Expand All @@ -1555,6 +1554,8 @@ int ecs_http_server_start(
ecs_check(srv->initialized, ECS_INVALID_PARAMETER, NULL);
ecs_check(!srv->should_run, ECS_INVALID_PARAMETER, NULL);
ecs_check(!srv->thread, ECS_INVALID_PARAMETER, NULL);
ecs_check(ecs_os_has_threading(), ECS_UNSUPPORTED,
"missing OS API implementation");

srv->should_run = true;

Expand Down Expand Up @@ -1583,6 +1584,8 @@ void ecs_http_server_stop(
"cannot stop HTTP server: not initialized");
ecs_check(srv->should_run, ECS_INVALID_PARAMETER,
"cannot stop HTTP server: already stopped/stopping");
ecs_check(ecs_os_has_threading(), ECS_UNSUPPORTED,
"missing OS API implementation");

/* Stop server thread */
ecs_dbg("http: shutting down server thread");
Expand Down

0 comments on commit b5b285d

Please sign in to comment.