diff options
| -rw-r--r-- | .travis.yml | 1 | ||||
| -rw-r--r-- | src/llthread.c | 34 | ||||
| -rw-r--r-- | test/test_logger.lua | 22 |
3 files changed, 39 insertions, 18 deletions
diff --git a/.travis.yml b/.travis.yml index 96cab16..7cadb6a 100644 --- a/.travis.yml +++ b/.travis.yml | |||
| @@ -51,6 +51,7 @@ script: | |||
| 51 | - lua$LUA_SFX test_join_timeout.lua | 51 | - lua$LUA_SFX test_join_timeout.lua |
| 52 | - lua$LUA_SFX test_join_detach.lua | 52 | - lua$LUA_SFX test_join_detach.lua |
| 53 | - lua$LUA_SFX test_register_ffi.lua | 53 | - lua$LUA_SFX test_register_ffi.lua |
| 54 | - lua$LUA_SFX test_logger.lua | ||
| 54 | 55 | ||
| 55 | notifications: | 56 | notifications: |
| 56 | email: | 57 | email: |
diff --git a/src/llthread.c b/src/llthread.c index 492766e..0ff39d7 100644 --- a/src/llthread.c +++ b/src/llthread.c | |||
| @@ -2,20 +2,21 @@ | |||
| 2 | # define USE_PTHREAD | 2 | # define USE_PTHREAD |
| 3 | #endif | 3 | #endif |
| 4 | 4 | ||
| 5 | #include <stdlib.h> | ||
| 6 | #include <memory.h> | ||
| 7 | #include <assert.h> | ||
| 8 | #include <errno.h> | ||
| 9 | |||
| 10 | #ifndef USE_PTHREAD | 5 | #ifndef USE_PTHREAD |
| 11 | # include <windows.h> | 6 | # include <windows.h> |
| 12 | # include <stdio.h> | ||
| 13 | # include <process.h> | 7 | # include <process.h> |
| 14 | #else | 8 | #else |
| 15 | # include <pthread.h> | 9 | # include <pthread.h> |
| 16 | # include <stdio.h> | ||
| 17 | #endif | 10 | #endif |
| 18 | 11 | ||
| 12 | #include <stdlib.h> | ||
| 13 | #include <stdio.h> | ||
| 14 | #include <memory.h> | ||
| 15 | #include <assert.h> | ||
| 16 | #include <errno.h> | ||
| 17 | #include <lualib.h> | ||
| 18 | #include "l52util.h" | ||
| 19 | |||
| 19 | /*export*/ | 20 | /*export*/ |
| 20 | #ifdef _WIN32 | 21 | #ifdef _WIN32 |
| 21 | # define LLTHREADS_EXPORT_API __declspec(dllexport) | 22 | # define LLTHREADS_EXPORT_API __declspec(dllexport) |
| @@ -40,7 +41,7 @@ | |||
| 40 | #endif | 41 | #endif |
| 41 | 42 | ||
| 42 | #ifndef USE_PTHREAD | 43 | #ifndef USE_PTHREAD |
| 43 | # define OS_THREAD_RETURT unsigned int __stdcall | 44 | # define OS_THREAD_RETURN unsigned int __stdcall |
| 44 | # define INVALID_THREAD INVALID_HANDLE_VALUE | 45 | # define INVALID_THREAD INVALID_HANDLE_VALUE |
| 45 | # define INFINITE_JOIN_TIMEOUT INFINITE | 46 | # define INFINITE_JOIN_TIMEOUT INFINITE |
| 46 | # define JOIN_OK 0 | 47 | # define JOIN_OK 0 |
| @@ -49,8 +50,7 @@ | |||
| 49 | typedef DWORD join_timeout_t; | 50 | typedef DWORD join_timeout_t; |
| 50 | typedef HANDLE os_thread_t; | 51 | typedef HANDLE os_thread_t; |
| 51 | #else | 52 | #else |
| 52 | # define OS_THREAD_RETURT void * | 53 | # define OS_THREAD_RETURN void * |
| 53 | # define INVALID_THREAD 0 | ||
| 54 | # define INFINITE_JOIN_TIMEOUT -1 | 54 | # define INFINITE_JOIN_TIMEOUT -1 |
| 55 | # define JOIN_OK 0 | 55 | # define JOIN_OK 0 |
| 56 | # define JOIN_ETIMEDOUT ETIMEDOUT | 56 | # define JOIN_ETIMEDOUT ETIMEDOUT |
| @@ -58,8 +58,6 @@ typedef int join_timeout_t; | |||
| 58 | typedef pthread_t os_thread_t; | 58 | typedef pthread_t os_thread_t; |
| 59 | #endif | 59 | #endif |
| 60 | 60 | ||
| 61 | #include "l52util.h" | ||
| 62 | #include <lualib.h> | ||
| 63 | 61 | ||
| 64 | LLTHREADS_EXPORT_API int luaopen_llthreads(lua_State *L); | 62 | LLTHREADS_EXPORT_API int luaopen_llthreads(lua_State *L); |
| 65 | 63 | ||
| @@ -389,10 +387,8 @@ static llthread_child_t *llthread_child_new() { | |||
| 389 | 387 | ||
| 390 | /* create new lua_State for the thread. */ | 388 | /* create new lua_State for the thread. */ |
| 391 | /* open standard libraries. */ | 389 | /* open standard libraries. */ |
| 392 | /* push traceback function as first value on stack. */ | ||
| 393 | this->L = luaL_newstate(); | 390 | this->L = luaL_newstate(); |
| 394 | open_thread_libs(this->L); | 391 | open_thread_libs(this->L); |
| 395 | lua_pushcfunction(this->L, traceback); | ||
| 396 | 392 | ||
| 397 | return this; | 393 | return this; |
| 398 | } | 394 | } |
| @@ -402,10 +398,14 @@ static void llthread_child_destroy(llthread_child_t *this) { | |||
| 402 | FREE_STRUCT(this); | 398 | FREE_STRUCT(this); |
| 403 | } | 399 | } |
| 404 | 400 | ||
| 405 | static OS_THREAD_RETURT llthread_child_thread_run(void *arg) { | 401 | static OS_THREAD_RETURN llthread_child_thread_run(void *arg) { |
| 406 | llthread_child_t *this = (llthread_child_t *)arg; | 402 | llthread_child_t *this = (llthread_child_t *)arg; |
| 407 | lua_State *L = this->L; | 403 | lua_State *L = this->L; |
| 408 | int nargs = lua_gettop(L) - 2; | 404 | int nargs = lua_gettop(L) - 1; |
| 405 | |||
| 406 | /* push traceback function as first value on stack. */ | ||
| 407 | lua_pushcfunction(this->L, traceback); | ||
| 408 | lua_insert(L, 1); | ||
| 409 | 409 | ||
| 410 | this->status = lua_pcall(L, nargs, LUA_MULTRET, 1); | 410 | this->status = lua_pcall(L, nargs, LUA_MULTRET, 1); |
| 411 | 411 | ||
| @@ -485,8 +485,6 @@ static int llthread_push_results(lua_State *L, llthread_child_t *child, int idx, | |||
| 485 | static int llthread_detach(llthread_t *this){ | 485 | static int llthread_detach(llthread_t *this){ |
| 486 | int rc = 0; | 486 | int rc = 0; |
| 487 | this->child = NULL; | 487 | this->child = NULL; |
| 488 | FLAG_SET(this, TSTATE_DETACHED); | ||
| 489 | FLAG_UNSET(this, FLAG_JOIN_LUA); | ||
| 490 | #ifdef USE_PTHREAD | 488 | #ifdef USE_PTHREAD |
| 491 | rc = pthread_detach(this->thread); | 489 | rc = pthread_detach(this->thread); |
| 492 | #endif | 490 | #endif |
diff --git a/test/test_logger.lua b/test/test_logger.lua new file mode 100644 index 0000000..66c190f --- /dev/null +++ b/test/test_logger.lua | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | require "llthreads".new([[ | ||
| 2 | require "string" | ||
| 3 | |||
| 4 | require "llthreads".set_logger(function(msg) | ||
| 5 | if type(msg) ~= 'string' then | ||
| 6 | print("ERROR! Invalid error message: ", msg) | ||
| 7 | os.exit(-2) | ||
| 8 | end | ||
| 9 | if not msg:find("SOME ERROR", nil, true) then | ||
| 10 | print("ERROR! Invalid error message: ", msg) | ||
| 11 | os.exit(-1) | ||
| 12 | end | ||
| 13 | print("Done!") | ||
| 14 | os.exit(0) | ||
| 15 | end) | ||
| 16 | |||
| 17 | error("SOME ERROR") | ||
| 18 | ]]):start():join() | ||
| 19 | |||
| 20 | print("ERROR! Logger has not been call!") | ||
| 21 | os.exit(-1) | ||
| 22 | |||
