Disable threads in WebVR;

Browser/emscripten support is currently unstable.
This commit is contained in:
bjorn 2018-04-14 21:36:24 -07:00 committed by bjornbytes
parent 2f6a25a1ff
commit 012ba07063
10 changed files with 48 additions and 25 deletions

View File

@ -161,7 +161,7 @@ elseif(WIN32)
else() else()
pkg_search_module(ODE REQUIRED ode) pkg_search_module(ODE REQUIRED ode)
include_directories(${ODE_INCLUDE_DIRS}) include_directories(${ODE_INCLUDE_DIRS})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lstdc++ -lpthread") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lstdc++")
if(APPLE) if(APPLE)
set(LOVR_ODE ode ccd) set(LOVR_ODE ode ccd)
else() else()
@ -216,6 +216,13 @@ if(NOT EMSCRIPTEN)
set(LOVR_OPENVR openvr_api) set(LOVR_OPENVR openvr_api)
endif() endif()
# pthreads
if(NOT WIN32 AND NOT EMSCRIPTEN)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(LOVR_PTHREADS Threads::Threads)
endif()
# LÖVR # LÖVR
set(LOVR_SRC set(LOVR_SRC
src/api/audio.c src/api/audio.c
@ -227,13 +234,11 @@ set(LOVR_SRC
src/api/lovr.c src/api/lovr.c
src/api/math.c src/api/math.c
src/api/physics.c src/api/physics.c
src/api/thread.c
src/api/timer.c src/api/timer.c
src/api/types/animator.c src/api/types/animator.c
src/api/types/audioStream.c src/api/types/audioStream.c
src/api/types/blob.c src/api/types/blob.c
src/api/types/canvas.c src/api/types/canvas.c
src/api/types/channel.c
src/api/types/collider.c src/api/types/collider.c
src/api/types/controller.c src/api/types/controller.c
src/api/types/font.c src/api/types/font.c
@ -249,7 +254,6 @@ set(LOVR_SRC
src/api/types/source.c src/api/types/source.c
src/api/types/texture.c src/api/types/texture.c
src/api/types/textureData.c src/api/types/textureData.c
src/api/types/thread.c
src/api/types/transform.c src/api/types/transform.c
src/api/types/vertexData.c src/api/types/vertexData.c
src/api/types/world.c src/api/types/world.c
@ -285,8 +289,6 @@ set(LOVR_SRC
src/lib/stb/stb_image.c src/lib/stb/stb_image.c
src/lib/stb/stb_image_write.c src/lib/stb/stb_image_write.c
src/lib/stb/stb_vorbis.c src/lib/stb/stb_vorbis.c
src/lib/tinycthread/tinycthread.c
src/lib/tinycthread/tinycthread.h
src/lib/vec/vec.c src/lib/vec/vec.c
src/lovr.c src/lovr.c
src/luax.c src/luax.c
@ -299,8 +301,6 @@ set(LOVR_SRC
src/math/vec3.c src/math/vec3.c
src/physics/physics.c src/physics/physics.c
src/resources/shaders.c src/resources/shaders.c
src/thread/channel.c
src/thread/thread.c
src/timer/timer.c src/timer/timer.c
src/util.c src/util.c
) )
@ -308,7 +308,15 @@ set(LOVR_SRC
if(EMSCRIPTEN) if(EMSCRIPTEN)
set(LOVR_SRC ${LOVR_SRC} src/headset/webvr.c) set(LOVR_SRC ${LOVR_SRC} src/headset/webvr.c)
else() else()
set(LOVR_SRC ${LOVR_SRC} src/headset/openvr.c) set(LOVR_SRC ${LOVR_SRC}
src/headset/openvr.c
src/api/thread.c
src/api/types/channel.c
src/api/types/thread.c
src/lib/tinycthread/tinycthread.c
src/thread/channel.c
src/thread/thread.c
)
endif() endif()
if(WIN32) if(WIN32)
@ -333,6 +341,7 @@ target_link_libraries(lovr
${LOVR_OPENGL} ${LOVR_OPENGL}
${LOVR_OPENVR} ${LOVR_OPENVR}
${LOVR_PHYSFS} ${LOVR_PHYSFS}
${LOVR_PTHREADS}
${LOVR_EMSCRIPTEN_FLAGS} ${LOVR_EMSCRIPTEN_FLAGS}
) )

View File

@ -31,11 +31,13 @@ static int nextEvent(lua_State* L) {
lua_pushboolean(L, event.data.mount.mounted); lua_pushboolean(L, event.data.mount.mounted);
return 2; return 2;
#ifndef EMSCRIPTEN
case EVENT_THREAD_ERROR: case EVENT_THREAD_ERROR:
luax_pushtype(L, Thread, event.data.threaderror.thread); luax_pushtype(L, Thread, event.data.threaderror.thread);
lua_pushstring(L, event.data.threaderror.error); lua_pushstring(L, event.data.threaderror.error);
free((void*) event.data.threaderror.error); free((void*) event.data.threaderror.error);
return 3; return 3;
#endif
case EVENT_CONTROLLER_ADDED: case EVENT_CONTROLLER_ADDED:
luax_pushtype(L, Controller, event.data.controlleradded.controller); luax_pushtype(L, Controller, event.data.controlleradded.controller);
@ -120,10 +122,12 @@ int l_lovrEventPush(lua_State* L) {
data.mount.mounted = lua_toboolean(L, 2); data.mount.mounted = lua_toboolean(L, 2);
break; break;
#ifndef EMSCRIPTEN
case EVENT_THREAD_ERROR: case EVENT_THREAD_ERROR:
data.threaderror.thread = luax_checktype(L, 2, Thread); data.threaderror.thread = luax_checktype(L, 2, Thread);
data.threaderror.error = luaL_checkstring(L, 3); data.threaderror.error = luaL_checkstring(L, 3);
break; break;
#endif
case EVENT_CONTROLLER_ADDED: case EVENT_CONTROLLER_ADDED:
data.controlleradded.controller = luax_checktype(L, 2, Controller); data.controlleradded.controller = luax_checktype(L, 2, Controller);

View File

@ -19,7 +19,11 @@ int l_lovrInit(lua_State* L) {
luax_preloadmodule(L, "lovr.headset", l_lovrHeadsetInit); luax_preloadmodule(L, "lovr.headset", l_lovrHeadsetInit);
luax_preloadmodule(L, "lovr.math", l_lovrMathInit); luax_preloadmodule(L, "lovr.math", l_lovrMathInit);
luax_preloadmodule(L, "lovr.physics", l_lovrPhysicsInit); luax_preloadmodule(L, "lovr.physics", l_lovrPhysicsInit);
#ifdef EMSCRIPTEN
luax_preloadmodule(L, "lovr.thread", luax_stubnil);
#else
luax_preloadmodule(L, "lovr.thread", l_lovrThreadInit); luax_preloadmodule(L, "lovr.thread", l_lovrThreadInit);
#endif
luax_preloadmodule(L, "lovr.timer", l_lovrTimerInit); luax_preloadmodule(L, "lovr.timer", l_lovrTimerInit);
luax_preloadmodule(L, "json", luaopen_cjson); luax_preloadmodule(L, "json", luaopen_cjson);
luax_preloadmodule(L, "enet", luaopen_enet); luax_preloadmodule(L, "enet", luaopen_enet);

View File

@ -1,6 +1,6 @@
#include "api.h" #include "api.h"
#include "thread/thread.h"
#include "event/event.h" #include "event/event.h"
#include "thread/thread.h"
static int threadRunner(void* data) { static int threadRunner(void* data) {
Thread* thread = (Thread*) data; Thread* thread = (Thread*) data;

View File

@ -8,7 +8,6 @@ void lovrEventInit() {
if (state.initialized) return; if (state.initialized) return;
vec_init(&state.pumps); vec_init(&state.pumps);
vec_init(&state.events); vec_init(&state.events);
mtx_init(&state.lock, mtx_plain);
lovrEventAddPump(glfwPollEvents); lovrEventAddPump(glfwPollEvents);
atexit(lovrEventDestroy); atexit(lovrEventDestroy);
state.initialized = true; state.initialized = true;
@ -18,20 +17,15 @@ void lovrEventDestroy() {
if (!state.initialized) return; if (!state.initialized) return;
vec_deinit(&state.pumps); vec_deinit(&state.pumps);
vec_deinit(&state.events); vec_deinit(&state.events);
mtx_destroy(&state.lock);
memset(&state, 0, sizeof(EventState)); memset(&state, 0, sizeof(EventState));
} }
void lovrEventAddPump(EventPump pump) { void lovrEventAddPump(EventPump pump) {
mtx_lock(&state.lock);
vec_push(&state.pumps, pump); vec_push(&state.pumps, pump);
mtx_unlock(&state.lock);
} }
void lovrEventRemovePump(EventPump pump) { void lovrEventRemovePump(EventPump pump) {
mtx_lock(&state.lock);
vec_remove(&state.pumps, pump); vec_remove(&state.pumps, pump);
mtx_unlock(&state.lock);
} }
void lovrEventPump() { void lovrEventPump() {
@ -42,25 +36,18 @@ void lovrEventPump() {
} }
void lovrEventPush(Event event) { void lovrEventPush(Event event) {
mtx_lock(&state.lock);
vec_insert(&state.events, 0, event); vec_insert(&state.events, 0, event);
mtx_unlock(&state.lock);
} }
bool lovrEventPoll(Event* event) { bool lovrEventPoll(Event* event) {
mtx_lock(&state.lock);
if (state.events.length == 0) { if (state.events.length == 0) {
mtx_unlock(&state.lock);
return false; return false;
} }
*event = vec_pop(&state.events); *event = vec_pop(&state.events);
mtx_unlock(&state.lock);
return true; return true;
} }
void lovrEventClear() { void lovrEventClear() {
mtx_lock(&state.lock);
vec_clear(&state.events); vec_clear(&state.events);
mtx_unlock(&state.lock);
} }

View File

@ -1,6 +1,7 @@
#include "headset/headset.h" #include "headset/headset.h"
#ifndef EMSCRIPTEN
#include "thread/thread.h" #include "thread/thread.h"
#include "lib/tinycthread/tinycthread.h" #endif
#include "lib/vec/vec.h" #include "lib/vec/vec.h"
#include <stdbool.h> #include <stdbool.h>
@ -30,10 +31,12 @@ typedef struct {
bool mounted; bool mounted;
} MountEvent; } MountEvent;
#ifndef EMSCRIPTEN
typedef struct { typedef struct {
Thread* thread; Thread* thread;
const char* error; const char* error;
} ThreadErrorEvent; } ThreadErrorEvent;
#endif
typedef struct { typedef struct {
Controller* controller; Controller* controller;
@ -57,7 +60,9 @@ typedef union {
QuitEvent quit; QuitEvent quit;
FocusEvent focus; FocusEvent focus;
MountEvent mount; MountEvent mount;
#ifndef EMSCRIPTEN
ThreadErrorEvent threaderror; ThreadErrorEvent threaderror;
#endif
ControllerAddedEvent controlleradded; ControllerAddedEvent controlleradded;
ControllerRemovedEvent controllerremoved; ControllerRemovedEvent controllerremoved;
ControllerPressedEvent controllerpressed; ControllerPressedEvent controllerpressed;
@ -78,7 +83,6 @@ typedef struct {
bool initialized; bool initialized;
vec_pump_t pumps; vec_pump_t pumps;
vec_event_t events; vec_event_t events;
mtx_t lock;
} EventState; } EventState;
void lovrEventInit(); void lovrEventInit();

View File

@ -5,6 +5,9 @@
#include "graphics/graphics.h" #include "graphics/graphics.h"
#include "math/math.h" #include "math/math.h"
#include "physics/physics.h" #include "physics/physics.h"
#ifndef EMSCRIPTEN
#include "thread/thread.h"
#endif
#include "timer/timer.h" #include "timer/timer.h"
void lovrDestroy() { void lovrDestroy() {
@ -15,7 +18,9 @@ void lovrDestroy() {
lovrHeadsetDestroy(); lovrHeadsetDestroy();
lovrMathDestroy(); lovrMathDestroy();
lovrPhysicsDestroy(); lovrPhysicsDestroy();
#ifndef EMSCRIPTEN
lovrThreadDeinit(); lovrThreadDeinit();
#endif
lovrTimerDestroy(); lovrTimerDestroy();
} }

View File

@ -40,6 +40,11 @@ int luax_preloadmodule(lua_State* L, const char* key, lua_CFunction f) {
return 0; return 0;
} }
int luax_stubnil(lua_State* L) {
lua_pushnil(L);
return 1;
}
void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions) { void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions) {
// Push metatable // Push metatable

View File

@ -30,6 +30,7 @@
else if (!luax_getobject(L, x)) { luax_newobject(L, T, x); } else if (!luax_getobject(L, x)) { luax_newobject(L, T, x); }
int luax_preloadmodule(lua_State* L, const char* key, lua_CFunction f); int luax_preloadmodule(lua_State* L, const char* key, lua_CFunction f);
int luax_stubnil(lua_State* L);
void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions); void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions);
void luax_extendtype(lua_State* L, const char* base, const char* name, const luaL_Reg* baseFunctions, const luaL_Reg* functions); void luax_extendtype(lua_State* L, const char* base, const char* name, const luaL_Reg* baseFunctions, const luaL_Reg* functions);
int luax_releasetype(lua_State* L); int luax_releasetype(lua_State* L);

View File

@ -1,5 +1,9 @@
#include "lib/vec/vec.h" #include "lib/vec/vec.h"
#ifdef EMSCRIPTEN
#define _Thread_local
#else
#include "lib/tinycthread/tinycthread.h" #include "lib/tinycthread/tinycthread.h"
#endif
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <setjmp.h> #include <setjmp.h>