Make State Send and Sync
ci/woodpecker/push/debug Pipeline was successful Details

This commit is contained in:
SeanOMik 2024-04-27 00:51:49 -04:00
parent 54c9926a04
commit a761f4094b
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
3 changed files with 46 additions and 5 deletions

View File

@ -20,7 +20,7 @@ impl<'a> Drop for LuaRef<'a> {
fn drop(&mut self) {
unsafe {
if Arc::strong_count(&self.lref) == 1 {
let s = self.state.lua.as_ptr();
let s = *self.state.lua.as_ptr();
lua::luaL_unref(s, lua::LUA_REGISTRYINDEX, *self.lref);
}
}

View File

@ -1,5 +1,5 @@
use core::ffi;
use std::{alloc::{self, Layout}, any::TypeId, cell::RefCell, collections::HashMap, ffi::{CStr, CString}, mem, ptr::{self, NonNull}, str::Utf8Error, sync::Arc};
use std::{alloc::{self, Layout}, any::TypeId, cell::RefCell, collections::HashMap, ffi::{CStr, CString}, mem, ptr::{self, NonNull}, str::Utf8Error, sync::{atomic::AtomicPtr, Arc}};
use lua::lua_gc;
use mlua_sys as lua;
@ -117,7 +117,8 @@ impl ExtraSpace {
#[derive(Clone)]
pub struct StatePtr {
pub lua: Arc<NonNull<lua::lua_State>>,
// arc is used here to make self Cloneable
pub lua: Arc<AtomicPtr<lua::lua_State>>,
}
pub struct State {
@ -167,7 +168,7 @@ impl State {
pub fn new() -> Self {
let s = Self {
ptr: StatePtr {
lua: Arc::new(unsafe { NonNull::new_unchecked(lua::luaL_newstate()) })
lua: Arc::new(AtomicPtr::new(unsafe { lua::luaL_newstate() }))
}
};
@ -196,7 +197,7 @@ impl State {
}
pub(crate) fn state_ptr(&self) -> *mut lua::lua_State {
self.ptr.lua.as_ptr()
unsafe { *self.ptr.lua.as_ptr() }
}
fn alloc_extra_space(&self) {

View File

@ -235,6 +235,46 @@ impl<'a> Table<'a> {
}
}
pub fn raw_seti<V>(&self, key: i64, val: V) -> Result<()>
where
V: AsLua<'a>
{
debug_assert!(key > 0, "Indexes in Lua start at 1");
let s = self.state.state_ptr();
unsafe {
self.state.ensure_stack(2)?;
let _g = StackGuard::new(self.state);
self.lref.push_to_lua_stack(self.state)?;
val.as_lua(self.state)?
.push_to_lua_stack(self.state)?;
lua::lua_rawseti(s, -2, key);
}
Ok(())
}
pub fn raw_geti<K, V>(&self, key: i64) -> Result<V>
where
V: FromLua<'a>,
{
debug_assert!(key > 0, "Indexes in Lua start at 1");
let s = self.state.state_ptr();
unsafe {
self.state.ensure_stack(1)?;
let _g = StackGuard::new(self.state);
self.lref.push_to_lua_stack(self.state)?;
lua::lua_rawgeti(s, -1, key);
let val = Value::from_lua_stack(self.state)?;
V::from_lua(self.state, val)
}
}
/// Returns a boolean indicating if this table has a key without calling any meta methods.
pub fn raw_has_key<K>(&self, key: K) -> Result<bool>
where