mirror of https://github.com/bjornbytes/lovr.git
rm strdup;
It isn't part of C, causing compiler errors on some platforms.
This commit is contained in:
parent
879c789a08
commit
7ebee200ea
|
@ -1,5 +1,6 @@
|
||||||
#include "api.h"
|
#include "api.h"
|
||||||
#include "event/event.h"
|
#include "event/event.h"
|
||||||
|
#include "thread/thread.h"
|
||||||
#include "core/platform.h"
|
#include "core/platform.h"
|
||||||
#include "core/ref.h"
|
#include "core/ref.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -105,7 +106,7 @@ static int nextEvent(lua_State* L) {
|
||||||
case EVENT_THREAD_ERROR:
|
case EVENT_THREAD_ERROR:
|
||||||
luax_pushtype(L, Thread, event.data.thread.thread);
|
luax_pushtype(L, Thread, event.data.thread.thread);
|
||||||
lua_pushstring(L, event.data.thread.error);
|
lua_pushstring(L, event.data.thread.error);
|
||||||
free(event.data.thread.error);
|
lovrRelease(Thread, event.data.thread.thread);
|
||||||
return 3;
|
return 3;
|
||||||
|
|
||||||
case EVENT_CUSTOM:
|
case EVENT_CUSTOM:
|
||||||
|
|
|
@ -13,10 +13,8 @@ static int threadRunner(void* data) {
|
||||||
lovrRetain(thread);
|
lovrRetain(thread);
|
||||||
mtx_lock(&thread->lock);
|
mtx_lock(&thread->lock);
|
||||||
thread->running = true;
|
thread->running = true;
|
||||||
thread->error = NULL;
|
|
||||||
mtx_unlock(&thread->lock);
|
mtx_unlock(&thread->lock);
|
||||||
|
|
||||||
// Lua state
|
|
||||||
lua_State* L = luaL_newstate();
|
lua_State* L = luaL_newstate();
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
lovrSetErrorCallback((errorFn*) luax_vthrow, L);
|
lovrSetErrorCallback((errorFn*) luax_vthrow, L);
|
||||||
|
@ -27,23 +25,28 @@ static int threadRunner(void* data) {
|
||||||
lua_pop(L, 2);
|
lua_pop(L, 2);
|
||||||
|
|
||||||
if (luaL_loadbuffer(L, thread->body->data, thread->body->size, "thread") || lua_pcall(L, 0, 0, 0)) {
|
if (luaL_loadbuffer(L, thread->body->data, thread->body->size, "thread") || lua_pcall(L, 0, 0, 0)) {
|
||||||
thread->error = lua_tostring(L, -1);
|
size_t length;
|
||||||
|
const char* error = lua_tolstring(L, -1, &length);
|
||||||
|
mtx_lock(&thread->lock);
|
||||||
|
thread->error = malloc(length + 1);
|
||||||
|
if (thread->error) {
|
||||||
|
memcpy(thread->error, error, length + 1);
|
||||||
|
lovrEventPush((Event) {
|
||||||
|
.type = EVENT_THREAD_ERROR,
|
||||||
|
.data.thread = { thread, thread->error }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
thread->running = false;
|
||||||
|
mtx_unlock(&thread->lock);
|
||||||
|
lovrRelease(Thread, thread);
|
||||||
|
lua_close(L);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
mtx_lock(&thread->lock);
|
mtx_lock(&thread->lock);
|
||||||
thread->running = false;
|
thread->running = false;
|
||||||
mtx_unlock(&thread->lock);
|
mtx_unlock(&thread->lock);
|
||||||
lovrRelease(Thread, thread);
|
lovrRelease(Thread, thread);
|
||||||
|
|
||||||
if (thread->error) {
|
|
||||||
lovrEventPush((Event) {
|
|
||||||
.type = EVENT_THREAD_ERROR,
|
|
||||||
.data.thread = { thread, strdup(thread->error) }
|
|
||||||
});
|
|
||||||
lua_close(L);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -54,10 +57,13 @@ static int l_lovrThreadNewThread(lua_State* L) {
|
||||||
size_t length;
|
size_t length;
|
||||||
const char* str = lua_tolstring(L, 1, &length);
|
const char* str = lua_tolstring(L, 1, &length);
|
||||||
if (memchr(str, '\n', MIN(1024, length))) {
|
if (memchr(str, '\n', MIN(1024, length))) {
|
||||||
blob = lovrBlobCreate(strdup(str), length, "thread code");
|
void* data = malloc(length + 1);
|
||||||
|
lovrAssert(data, "Out of memory");
|
||||||
|
memcpy(data, str, length + 1);
|
||||||
|
blob = lovrBlobCreate(data, length, "thread code");
|
||||||
} else {
|
} else {
|
||||||
void* code = lovrFilesystemRead(str, -1, &length);
|
void* code = lovrFilesystemRead(str, -1, &length);
|
||||||
lovrAssert(code, "Could not read thread code from %s", str);
|
lovrAssert(code, "Could not read thread code from file '%s'", str);
|
||||||
blob = lovrBlobCreate(code, length, str);
|
blob = lovrBlobCreate(code, length, str);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include "core/arr.h"
|
#include "core/arr.h"
|
||||||
#include "core/ref.h"
|
#include "core/ref.h"
|
||||||
|
#include "core/util.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
@ -36,6 +37,10 @@ void lovrEventPump() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrEventPush(Event event) {
|
void lovrEventPush(Event event) {
|
||||||
|
if (event.type == EVENT_THREAD_ERROR) {
|
||||||
|
lovrRetain(event.data.thread.thread);
|
||||||
|
}
|
||||||
|
|
||||||
arr_push(&state.events, event);
|
arr_push(&state.events, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -432,7 +432,10 @@ void bridgeLovrInit(BridgeLovrInitData *initData) {
|
||||||
bridgeLovrMobileData.deviceType = initData->deviceType;
|
bridgeLovrMobileData.deviceType = initData->deviceType;
|
||||||
|
|
||||||
free(apkPath);
|
free(apkPath);
|
||||||
apkPath = strdup(initData->apkPath);
|
size_t length = strlen(initData->apkPath);
|
||||||
|
apkPath = malloc(length + 1);
|
||||||
|
lovrAssert(apkPath, "Out of memory");
|
||||||
|
memcpy(apkPath, initData->apkPath, length + 1);
|
||||||
|
|
||||||
bridgeLovrInitState();
|
bridgeLovrInitState();
|
||||||
|
|
||||||
|
|
|
@ -44,8 +44,6 @@ Thread* lovrThreadInit(Thread* thread, int (*runner)(void*), Blob* body) {
|
||||||
lovrRetain(body);
|
lovrRetain(body);
|
||||||
thread->runner = runner;
|
thread->runner = runner;
|
||||||
thread->body = body;
|
thread->body = body;
|
||||||
thread->error = NULL;
|
|
||||||
thread->running = false;
|
|
||||||
mtx_init(&thread->lock, mtx_plain);
|
mtx_init(&thread->lock, mtx_plain);
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
@ -55,6 +53,7 @@ void lovrThreadDestroy(void* ref) {
|
||||||
mtx_destroy(&thread->lock);
|
mtx_destroy(&thread->lock);
|
||||||
thrd_detach(thread->handle);
|
thrd_detach(thread->handle);
|
||||||
lovrRelease(Blob, thread->body);
|
lovrRelease(Blob, thread->body);
|
||||||
|
free(thread->error);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrThreadStart(Thread* thread) {
|
void lovrThreadStart(Thread* thread) {
|
||||||
|
@ -64,10 +63,9 @@ void lovrThreadStart(Thread* thread) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (thrd_create(&thread->handle, thread->runner, thread) != thrd_success) {
|
free(thread->error);
|
||||||
lovrThrow("Could not create thread...sorry");
|
thread->error = NULL;
|
||||||
return;
|
lovrAssert(thrd_create(&thread->handle, thread->runner, thread) == thrd_success, "Could not create thread...sorry");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void lovrThreadWait(Thread* thread) {
|
void lovrThreadWait(Thread* thread) {
|
||||||
|
|
|
@ -11,7 +11,7 @@ typedef struct Thread {
|
||||||
mtx_t lock;
|
mtx_t lock;
|
||||||
Blob* body;
|
Blob* body;
|
||||||
int (*runner)(void*);
|
int (*runner)(void*);
|
||||||
const char* error;
|
char* error;
|
||||||
bool running;
|
bool running;
|
||||||
} Thread;
|
} Thread;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue