diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/llthread.c | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/src/llthread.c b/src/llthread.c index 0449ea9..0f43e14 100644 --- a/src/llthread.c +++ b/src/llthread.c | |||
| @@ -43,12 +43,16 @@ | |||
| 43 | # define OS_THREAD_RETURT unsigned int __stdcall | 43 | # define OS_THREAD_RETURT unsigned int __stdcall |
| 44 | # define INVALID_THREAD INVALID_HANDLE_VALUE | 44 | # define INVALID_THREAD INVALID_HANDLE_VALUE |
| 45 | # define INFINITE_JOIN_TIMEOUT INFINITE | 45 | # define INFINITE_JOIN_TIMEOUT INFINITE |
| 46 | # define JOIN_OK 0 | ||
| 47 | # define JOIN_ETIMEDOUT 1 | ||
| 46 | typedef DWORD join_timeout_t; | 48 | typedef DWORD join_timeout_t; |
| 47 | typedef HANDLE os_thread_t; | 49 | typedef HANDLE os_thread_t; |
| 48 | #else | 50 | #else |
| 49 | # define OS_THREAD_RETURT void * | 51 | # define OS_THREAD_RETURT void * |
| 50 | # define INVALID_THREAD 0 | 52 | # define INVALID_THREAD 0 |
| 51 | # define INFINITE_JOIN_TIMEOUT -1 | 53 | # define INFINITE_JOIN_TIMEOUT -1 |
| 54 | # define JOIN_OK 0 | ||
| 55 | # define JOIN_ETIMEDOUT ETIMEDOUT | ||
| 52 | typedef int join_timeout_t; | 56 | typedef int join_timeout_t; |
| 53 | typedef pthread_t os_thread_t; | 57 | typedef pthread_t os_thread_t; |
| 54 | #endif | 58 | #endif |
| @@ -321,13 +325,15 @@ static void open_thread_libs(lua_State *L){ | |||
| 321 | /* get package.preload */ | 325 | /* get package.preload */ |
| 322 | lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); lua_remove(L, -2); | 326 | lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); lua_remove(L, -2); |
| 323 | 327 | ||
| 328 | /*always only register*/ | ||
| 329 | lua_pushcfunction(L, luaopen_llthreads); lua_setfield(L, -2, "llthreads"); | ||
| 330 | |||
| 324 | L_REGLIB(L, io, 1); | 331 | L_REGLIB(L, io, 1); |
| 325 | L_REGLIB(L, os, 1); | 332 | L_REGLIB(L, os, 1); |
| 326 | L_REGLIB(L, math, 1); | 333 | L_REGLIB(L, math, 1); |
| 327 | L_REGLIB(L, table, 1); | 334 | L_REGLIB(L, table, 1); |
| 328 | L_REGLIB(L, debug, 1); | 335 | L_REGLIB(L, debug, 1); |
| 329 | L_REGLIB(L, string, 1); | 336 | L_REGLIB(L, string, 1); |
| 330 | L_REGLIB(L, llthreads, 0); | ||
| 331 | 337 | ||
| 332 | lua_settop(L, top); | 338 | lua_settop(L, top); |
| 333 | #undef L_REGLIB | 339 | #undef L_REGLIB |
| @@ -482,9 +488,24 @@ static int llthread_join(llthread_t *this, join_timeout_t timeout) { | |||
| 482 | } | 488 | } |
| 483 | return 2; | 489 | return 2; |
| 484 | #else | 490 | #else |
| 491 | int rc; | ||
| 492 | if(timeout == 0){ | ||
| 493 | rc = pthread_kill(this->thread, 0); | ||
| 494 | if(rc == 0){ /* still alive */ | ||
| 495 | rc = ETIMEDOUT; | ||
| 496 | } | ||
| 497 | if(rc == ESRCH){ /*thread dead*/ | ||
| 498 | FLAG_SET(this, TSTATE_JOINED); | ||
| 499 | rc = 0; | ||
| 500 | } | ||
| 501 | return rc; | ||
| 502 | } | ||
| 503 | |||
| 504 | // @todo use pthread_tryjoin_np to support timeout | ||
| 505 | |||
| 485 | /* then join the thread. */ | 506 | /* then join the thread. */ |
| 486 | int rc = pthread_join(this->thread, NULL); | 507 | rc = pthread_join(this->thread, NULL); |
| 487 | if(rc == 0) { | 508 | if((rc == 0) || (rc == ESRCH)) { |
| 488 | FLAG_SET(this, TSTATE_JOINED); | 509 | FLAG_SET(this, TSTATE_JOINED); |
| 489 | } | 510 | } |
| 490 | return rc; | 511 | return rc; |
| @@ -598,7 +619,7 @@ static int l_llthread_join(lua_State *L) { | |||
| 598 | } | 619 | } |
| 599 | 620 | ||
| 600 | /* join the thread. */ | 621 | /* join the thread. */ |
| 601 | rc = llthread_join(this, INFINITE_JOIN_TIMEOUT); | 622 | rc = llthread_join(this, luaL_optint(L, 2, INFINITE_JOIN_TIMEOUT)); |
| 602 | 623 | ||
| 603 | /* Push all results after the Lua code. */ | 624 | /* Push all results after the Lua code. */ |
| 604 | if(child && FLAG_IS_SET(this, TSTATE_JOINED)) { | 625 | if(child && FLAG_IS_SET(this, TSTATE_JOINED)) { |
| @@ -618,13 +639,11 @@ static int l_llthread_join(lua_State *L) { | |||
| 618 | return top; | 639 | return top; |
| 619 | } | 640 | } |
| 620 | 641 | ||
| 621 | #ifndef USE_PTHREAD | 642 | if( rc == JOIN_ETIMEDOUT ){ |
| 622 | if( rc == 1 ){ | ||
| 623 | lua_pushboolean(L, 0); | 643 | lua_pushboolean(L, 0); |
| 624 | lua_pushstring(L, "timeout"); | 644 | lua_pushstring(L, "timeout"); |
| 625 | return 2; | 645 | return 2; |
| 626 | } | 646 | } |
| 627 | #endif | ||
| 628 | 647 | ||
| 629 | { | 648 | { |
| 630 | char buf[ERROR_LEN]; | 649 | char buf[ERROR_LEN]; |
