2626#include "logqueue.h"
2727#include "systime.h"
2828#include "threadsig.h"
29+ #include "semaphore.h"
2930
3031LUAMOD_API int luaopen_ltask (lua_State * L );
3132LUAMOD_API int luaopen_ltask_bootstrap (lua_State * L );
@@ -52,23 +53,23 @@ LUAMOD_API int luaopen_ltask_root(lua_State *L);
5253
5354struct mainthread_session {
5455 atomic_int srv ;
55- struct cond ev ;
56+ struct sem ev ;
5657 int status ;
5758};
5859
5960static void
6061mainthread_init (struct mainthread_session * mt ) {
6162 atomic_int_init (& mt -> srv , 0 );
6263 mt -> status = MAINTHREAD_STATUS_NONE ;
63- cond_create (& mt -> ev );
64+ sem_init (& mt -> ev );
6465}
6566
6667static void
6768mainthread_deinit (struct mainthread_session * mt ) {
6869 assert (mt -> status == MAINTHREAD_STATUS_NONE || mt -> status == MAINTHREAD_STATUS_YIELD );
6970 atomic_int_store (& mt -> srv , 0 );
7071 mt -> status = MAINTHREAD_STATUS_INVALID ;
71- cond_release (& mt -> ev );
72+ sem_deinit (& mt -> ev );
7273}
7374
7475static inline int
@@ -78,8 +79,7 @@ mainthread_current(struct mainthread_session *mt, service_id id) {
7879
7980static inline void
8081mainthread_trigger (struct mainthread_session * mt ) {
81- cond_trigger_begin (& mt -> ev );
82- cond_trigger_end (& mt -> ev , 1 );
82+ sem_release (& mt -> ev );
8383}
8484
8585struct ltask {
@@ -1153,11 +1153,13 @@ lmainthread_wait(lua_State *L) {
11531153 if (mt -> status != MAINTHREAD_STATUS_NONE && mt -> status != MAINTHREAD_STATUS_READY )
11541154 return luaL_error (L , "mainthread in use" );
11551155 struct service_pool * P = task -> services ;
1156+ int inf = !lua_toboolean (L , 1 );
11561157 int finish = 0 ;
11571158 do {
1158- cond_wait_begin (& mt -> ev );
1159- cond_wait (& mt -> ev );
1160- cond_wait_end (& mt -> ev );
1159+ if (sem_acquire (& mt -> ev , inf )) {
1160+ lua_pushboolean (L , 1 ); // fail
1161+ return 1 ;
1162+ }
11611163 if (mt -> status != MAINTHREAD_STATUS_READY )
11621164 luaL_error (L , "mainthread not ready" );
11631165
@@ -1196,7 +1198,8 @@ lmainthread_wait(lua_State *L) {
11961198 schedule_back (task , id );
11971199 mt -> status = MAINTHREAD_STATUS_NONE ;
11981200 } while (!finish );
1199- atomic_int_store (& mt -> srv , 0 ); return 0 ;
1201+ atomic_int_store (& mt -> srv , 0 );
1202+ return 0 ;
12001203}
12011204
12021205LUAMOD_API int
0 commit comments