aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/thread/buffer.md31
-rw-r--r--src/thread.c5
2 files changed, 35 insertions, 1 deletions
diff --git a/docs/thread/buffer.md b/docs/thread/buffer.md
new file mode 100644
index 0000000..e366a33
--- /dev/null
+++ b/docs/thread/buffer.md
@@ -0,0 +1,31 @@
+## buffer
+
+thread.buffer(V)
+
+a buffer is a container that allows a variable to be shared between threads & states in a thread safe manor.
+
+it is able to store 'anything' though lightuserdata will likely not work properly. poorly structured user data may retain some shared state with the original, which could cause some use-after-free in some really poor situations. providing a __copy metamethod will alleviate this issue (read more in readme.md)
+
+the __gc metamethod will be stripped from the original object and called when the buffer's __gc gets called. you should not reuse the original object after putting in a buffer for this reason.
+
+the __index metamethod will index any value that is not a buffer.* method on the original object (i will try not to add any more)
+
+every other metamethod will be replaced with a proxy to the metamethod in the copied object
+
+### buffer:get
+
+buffer:get()
+
+copies the value in the buffer to the current state
+
+### buffer:set
+
+buffer:set(V)
+
+sets the value in the buffer
+
+### buffer:mod
+
+buffer:mod(function(V))
+
+takes a function with a single argument (the value), the return value of this will be the new value, if it is nil, the value will return unchanged
diff --git a/src/thread.c b/src/thread.c
index 617d63f..7d48bda 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -434,6 +434,7 @@ int l_buffer_index(lua_State* L){
hash = fnv_1((uint8_t*)str, len, v_1);
+ //maybe strcmp after the hash has been verified?
switch(hash){
case 0xd8c8ad186b9ed323: //get
lua_pushcfunction(L, _buffer_get);
@@ -464,10 +465,10 @@ int hi(lua_State* L){
return 0;
}
-//not thread safe yet
int meta_proxy(lua_State* L){
int argc = lua_gettop(L);
struct thread_buffer *buffer = lua_touserdata(L, 1);
+ pthread_mutex_lock(&*buffer->lock);
lua_getmetatable(buffer->L, 1);
lua_pushstring(buffer->L, lua_tostring(L, 2));
@@ -490,10 +491,12 @@ int meta_proxy(lua_State* L){
lua_setmetatable(buffer->L, -2);
lua_settop(buffer->L, 1);
+ pthread_mutex_unlock(&*buffer->lock);
//printf("%p\n", lua_topointer(buffer->L, -1));
return 1;
}
+#warning "make this reapply for new objects!"
void meta_proxy_gen(lua_State* L, struct thread_buffer *buffer, int meta_idx, int new_meta_idx){
lua_pushcfunction(L, meta_proxy);