@@ -19,46 +19,37 @@ namespace bee::lua_channel {
1919 using box = std::shared_ptr<channel>;
2020
2121 bool init () noexcept {
22- if (!ev.open ()) {
23- return false ;
24- }
25- return true ;
22+ return ev.open ();
2623 }
2724 net::fd_t fd () const noexcept {
2825 return ev.fd ();
2926 }
30- void push (lua_State* L, int from) noexcept {
31- void * data = seri_pack (L, from, NULL );
27+ void push (void * data) noexcept {
3228 std::unique_lock<spinlock> lk (mutex);
3329 queue.push (data);
3430 ev.set ();
3531 }
36- int pop (lua_State* L) noexcept {
37- void * data;
38- {
39- std::unique_lock<spinlock> lk (mutex);
40- if (queue.empty ()) {
41- ev.clear ();
42- lua_pushboolean (L, 0 );
43- return 1 ;
44- }
45- data = queue.front ();
46- queue.pop ();
32+ void * pop () noexcept {
33+ std::unique_lock<spinlock> lk (mutex);
34+ if (queue.empty ()) {
35+ ev.clear ();
36+ return nullptr ;
4737 }
48- lua_pushboolean (L, 1 );
49- return 1 + seri_unpackptr (L, data);
38+ void * data = queue.front ();
39+ queue.pop ();
40+ if (queue.empty ()) {
41+ ev.clear ();
42+ }
43+ return data;
5044 }
5145 void clear () noexcept {
5246 std::unique_lock<spinlock> lk (mutex);
53- for (;;) {
54- if (queue.empty ()) {
55- ev.clear ();
56- return ;
57- }
47+ while (!queue.empty ()) {
5848 void * data = queue.front ();
5949 free (data);
6050 queue.pop ();
6151 }
52+ ev.clear ();
6253 }
6354
6455 private:
@@ -110,14 +101,21 @@ namespace bee::lua_channel {
110101 static channelmgr g_channel;
111102
112103 static int lchannel_push (lua_State* L) {
113- auto & bc = lua::checkudata<channel::box>(L, 1 );
114- bc->push (L, 1 );
104+ auto & bc = lua::checkudata<channel::box>(L, 1 );
105+ void * data = seri_pack (L, 1 , NULL );
106+ bc->push (data);
115107 return 0 ;
116108 }
117109
118110 static int lchannel_pop (lua_State* L) {
119- auto & bc = lua::checkudata<channel::box>(L, 1 );
120- return bc->pop (L);
111+ auto & bc = lua::checkudata<channel::box>(L, 1 );
112+ void * data = bc->pop ();
113+ if (!data) {
114+ lua_pushboolean (L, 0 );
115+ return 1 ;
116+ }
117+ lua_pushboolean (L, 1 );
118+ return 1 + seri_unpackptr (L, data);
121119 }
122120
123121 static int lchannel_fd (lua_State* L) {
0 commit comments