@@ -8,8 +8,6 @@ use std::result::Result as StdResult;
88use std:: sync:: Arc ;
99use std:: { mem, ptr} ;
1010
11- use parking_lot:: ReentrantMutex ;
12-
1311use crate :: chunk:: ChunkMode ;
1412use crate :: error:: { Error , Result } ;
1513use crate :: function:: Function ;
@@ -21,7 +19,7 @@ use crate::table::Table;
2119use crate :: thread:: Thread ;
2220use crate :: types:: {
2321 AppDataRef , AppDataRefMut , Callback , CallbackUpvalue , DestructedUserdata , Integer ,
24- LightUserData , MaybeSend , RegistryKey , SubtypeId , ValueRef ,
22+ LightUserData , MaybeSend , ReentrantMutex , RegistryKey , SubtypeId , ValueRef , XRc ,
2523} ;
2624use crate :: userdata:: { AnyUserData , MetaMethod , UserData , UserDataRegistry , UserDataVariant } ;
2725use crate :: util:: {
@@ -50,7 +48,7 @@ pub struct RawLua {
5048 // The state is dynamic and depends on context
5149 pub ( super ) state : Cell < * mut ffi:: lua_State > ,
5250 pub ( super ) main_state : * mut ffi:: lua_State ,
53- pub ( super ) extra : Rc < UnsafeCell < ExtraData > > ,
51+ pub ( super ) extra : XRc < UnsafeCell < ExtraData > > ,
5452}
5553
5654#[ cfg( not( feature = "module" ) ) ]
@@ -69,6 +67,9 @@ impl Drop for RawLua {
6967 }
7068}
7169
70+ #[ cfg( feature = "send" ) ]
71+ unsafe impl Send for RawLua { }
72+
7273impl RawLua {
7374 #[ inline( always) ]
7475 pub ( crate ) fn lua ( & self ) -> & Lua {
@@ -96,7 +97,7 @@ impl RawLua {
9697 unsafe { ( * self . extra . get ( ) ) . ref_thread }
9798 }
9899
99- pub ( super ) unsafe fn new ( libs : StdLib , options : LuaOptions ) -> Arc < ReentrantMutex < Self > > {
100+ pub ( super ) unsafe fn new ( libs : StdLib , options : LuaOptions ) -> XRc < ReentrantMutex < Self > > {
100101 let mem_state: * mut MemoryState = Box :: into_raw ( Box :: default ( ) ) ;
101102 let mut state = ffi:: lua_newstate ( ALLOCATOR , mem_state as * mut c_void ) ;
102103 // If state is null then switch to Lua internal allocator
@@ -154,7 +155,7 @@ impl RawLua {
154155 rawlua
155156 }
156157
157- pub ( super ) unsafe fn init_from_ptr ( state : * mut ffi:: lua_State ) -> Arc < ReentrantMutex < Self > > {
158+ pub ( super ) unsafe fn init_from_ptr ( state : * mut ffi:: lua_State ) -> XRc < ReentrantMutex < Self > > {
158159 assert ! ( !state. is_null( ) , "Lua state is NULL" ) ;
159160 if let Some ( lua) = Self :: try_from_ptr ( state) {
160161 return lua;
@@ -209,10 +210,10 @@ impl RawLua {
209210 assert_stack ( main_state, ffi:: LUA_MINSTACK ) ;
210211
211212 #[ allow( clippy:: arc_with_non_send_sync) ]
212- let rawlua = Arc :: new ( ReentrantMutex :: new ( RawLua {
213+ let rawlua = XRc :: new ( ReentrantMutex :: new ( RawLua {
213214 state : Cell :: new ( state) ,
214215 main_state,
215- extra : Rc :: clone ( & extra) ,
216+ extra : XRc :: clone ( & extra) ,
216217 } ) ) ;
217218 ( * extra. get ( ) ) . set_lua ( & rawlua) ;
218219
@@ -221,10 +222,10 @@ impl RawLua {
221222
222223 pub ( super ) unsafe fn try_from_ptr (
223224 state : * mut ffi:: lua_State ,
224- ) -> Option < Arc < ReentrantMutex < Self > > > {
225+ ) -> Option < XRc < ReentrantMutex < Self > > > {
225226 match ExtraData :: get ( state) {
226227 extra if extra. is_null ( ) => None ,
227- extra => Some ( Arc :: clone ( & ( * extra) . lua ( ) . 0 ) ) ,
228+ extra => Some ( XRc :: clone ( & ( * extra) . lua ( ) . 0 ) ) ,
228229 }
229230 }
230231
@@ -369,7 +370,7 @@ impl RawLua {
369370 callback_error_ext ( state, extra, move |_| {
370371 let hook_cb = ( * extra) . hook_callback . clone ( ) ;
371372 let hook_cb = mlua_expect ! ( hook_cb, "no hook callback set in hook_proc" ) ;
372- if Arc :: strong_count ( & hook_cb) > 2 {
373+ if Rc :: strong_count ( & hook_cb) > 2 {
373374 return Ok ( ( ) ) ; // Don't allow recursion
374375 }
375376 let rawlua = ( * extra) . raw_lua ( ) ;
@@ -379,7 +380,7 @@ impl RawLua {
379380 } )
380381 }
381382
382- ( * self . extra . get ( ) ) . hook_callback = Some ( Arc :: new ( callback) ) ;
383+ ( * self . extra . get ( ) ) . hook_callback = Some ( Rc :: new ( callback) ) ;
383384 ( * self . extra . get ( ) ) . hook_thread = state; // Mark for what thread the hook is set
384385 ffi:: lua_sethook ( state, Some ( hook_proc) , triggers. mask ( ) , triggers. count ( ) ) ;
385386 }
@@ -1105,7 +1106,7 @@ impl RawLua {
11051106 check_stack ( state, 4 ) ?;
11061107
11071108 let func = mem:: transmute :: < Callback , Callback < ' static > > ( func) ;
1108- let extra = Rc :: clone ( & self . extra ) ;
1109+ let extra = XRc :: clone ( & self . extra ) ;
11091110 let protect = !self . unlikely_memory_error ( ) ;
11101111 push_gc_userdata ( state, CallbackUpvalue { data : func, extra } , protect) ?;
11111112 if protect {
@@ -1149,7 +1150,7 @@ impl RawLua {
11491150 let args = MultiValue :: from_stack_multi ( nargs, rawlua) ?;
11501151 let func = & * ( * upvalue) . data ;
11511152 let fut = func ( rawlua, args) ;
1152- let extra = Rc :: clone ( & ( * upvalue) . extra ) ;
1153+ let extra = XRc :: clone ( & ( * upvalue) . extra ) ;
11531154 let protect = !rawlua. unlikely_memory_error ( ) ;
11541155 push_gc_userdata ( state, AsyncPollUpvalue { data : fut, extra } , protect) ?;
11551156 if protect {
@@ -1209,7 +1210,7 @@ impl RawLua {
12091210 check_stack ( state, 4 ) ?;
12101211
12111212 let func = mem:: transmute :: < AsyncCallback , AsyncCallback < ' static > > ( func) ;
1212- let extra = Rc :: clone ( & self . extra ) ;
1213+ let extra = XRc :: clone ( & self . extra ) ;
12131214 let protect = !self . unlikely_memory_error ( ) ;
12141215 let upvalue = AsyncCallbackUpvalue { data : func, extra } ;
12151216 push_gc_userdata ( state, upvalue, protect) ?;
0 commit comments