@@ -21,7 +21,7 @@ use crate::thread::Thread;
21
21
use crate :: traits:: { FromLua , FromLuaMulti , IntoLua , IntoLuaMulti } ;
22
22
use crate :: types:: {
23
23
AppDataRef , AppDataRefMut , ArcReentrantMutexGuard , Integer , LuaType , MaybeSend , Number , ReentrantMutex ,
24
- ReentrantMutexGuard , RegistryKey , VmState , XRc , XWeak ,
24
+ ReentrantMutexGuard , RegistryKey , VmState , XRc , XWeak , ThreadEventInfo
25
25
} ;
26
26
use crate :: userdata:: { AnyUserData , UserData , UserDataProxy , UserDataRegistry , UserDataStorage } ;
27
27
use crate :: util:: {
@@ -671,6 +671,71 @@ impl Lua {
671
671
}
672
672
}
673
673
674
+ /// Sets a callback that will be called by Luau whenever a thread is created/destroyed.
675
+ ///
676
+ /// Often used for keeping track of threads.
677
+ #[ cfg( any( feature = "luau" , doc) ) ]
678
+ #[ cfg_attr( docsrs, doc( cfg( feature = "luau" ) ) ) ]
679
+ pub fn set_thread_event_callback < F > ( & self , callback : F )
680
+ where
681
+ F : Fn ( & Lua , ThreadEventInfo ) -> Result < ( ) > + MaybeSend + ' static ,
682
+ {
683
+ use std:: rc:: Rc ;
684
+
685
+ unsafe extern "C-unwind" fn userthread_proc ( parent : * mut ffi:: lua_State , state : * mut ffi:: lua_State ) {
686
+ callback_error_ext ( state, ptr:: null_mut ( ) , move |extra, _| {
687
+ let raw_lua: & RawLua = ( * extra) . raw_lua ( ) ;
688
+ let _guard = StateGuard :: new ( raw_lua, state) ;
689
+
690
+ let userthread_cb = ( * extra) . userthread_callback . clone ( ) ;
691
+ let userthread_cb = mlua_expect ! ( userthread_cb, "no userthread callback set in userthread_proc" ) ;
692
+ if parent. is_null ( ) {
693
+ raw_lua. push ( Value :: Nil ) . unwrap ( ) ;
694
+ } else {
695
+ raw_lua. push_ref_thread ( parent) . unwrap ( ) ;
696
+ }
697
+ if parent. is_null ( ) {
698
+ let event_info = ThreadEventInfo :: Destroyed ( state. cast_const ( ) . cast ( ) ) ;
699
+ let main_state = raw_lua. main_state ( ) ;
700
+ if main_state == state {
701
+ return Ok ( ( ) ) ; // Don't process Destroyed event on main thread.
702
+ }
703
+ let main_extra = ExtraData :: get ( main_state) ;
704
+ let main_raw_lua: & RawLua = ( * main_extra) . raw_lua ( ) ;
705
+ let _guard = StateGuard :: new ( main_raw_lua, state) ;
706
+ userthread_cb ( ( * main_extra) . lua ( ) , event_info)
707
+ } else {
708
+ raw_lua. push_ref_thread ( parent) . unwrap ( ) ;
709
+ let event_info = match raw_lua. pop_value ( ) {
710
+ Value :: Thread ( thr) => ThreadEventInfo :: Created ( thr) ,
711
+ _ => unimplemented ! ( )
712
+ } ;
713
+ userthread_cb ( ( * extra) . lua ( ) , event_info)
714
+ }
715
+ } ) ;
716
+ }
717
+
718
+ // Set interrupt callback
719
+ let lua = self . lock ( ) ;
720
+ unsafe {
721
+ ( * lua. extra . get ( ) ) . userthread_callback = Some ( Rc :: new ( callback) ) ;
722
+ ( * ffi:: lua_callbacks ( lua. main_state ( ) ) ) . userthread = Some ( userthread_proc) ;
723
+ }
724
+ }
725
+
726
+ /// Removes any thread event function previously set by `set_thread_event_callback`.
727
+ ///
728
+ /// This function has no effect if a callback was not previously set.
729
+ #[ cfg( any( feature = "luau" , doc) ) ]
730
+ #[ cfg_attr( docsrs, doc( cfg( feature = "luau" ) ) ) ]
731
+ pub fn remove_thread_event_callback ( & self ) {
732
+ let lua = self . lock ( ) ;
733
+ unsafe {
734
+ ( * lua. extra . get ( ) ) . userthread_callback = None ;
735
+ ( * ffi:: lua_callbacks ( lua. main_state ( ) ) ) . userthread = None ;
736
+ }
737
+ }
738
+
674
739
/// Sets the warning function to be used by Lua to emit warnings.
675
740
///
676
741
/// Requires `feature = "lua54"`
0 commit comments