Coder Social home page Coder Social logo

luathreading's Introduction

LuaThreading

This module was created for fixing problem with multi threading in Lua. It supports creating dedicated lua state, and syncing main lua thread with another thread.

How to use

Include LuaThreading into your project

CreateProject(...)
    -- ...
    IncludeLuaShared() -- Do not forget to include LuaShared
    IncludeLuaThreading()
    -- ...

If you're using CMake:

target_include_libraries(<your_library> PRIVATE LuaThreading)
# P.S: Use my CMake fork of garrysmod_common

Initialize LuaThreading

#include <lua_threading.hpp>

GMOD_MODULE_OPEN() {
    LuaThreading::Initialize(LUA);

    // Register think somewhere here
    // ...

    return 0;
}

GMOD_MODULE_CLOSE() {
	LuaThreading::Deinitialize(LUA);

    // ...

	return 0;
}

Registering think function for thread syncing

LUA->GetField(GarrysMod::Lua::INDEX_GLOBAL, "timer");
LUA->GetField(-1, "Create");
LUA->PushString("my_super_think"); // identifier
LUA->PushNumber(0); // delay
LUA->PushNumber(0); // repetitions
LUA->PushCFunction(LuaThreading::Think);
LUA->Call(4, 0);
LUA->Pop();

// Or if you already have think function then just call think

LuaThreading::Think(LUA);

Acquiring lua state from anywhere

{
    // Getting lua state (and syncing threads if needed)
    LuaThreading::Lua LUA = LuaThreading::AcquireLua(); // Or LuaThreading::Lua();
    // Using Lua as Garrysmod::Lua::ILuaBase*
    LUA->GetField(GarrysMod::Lua::INDEX_GLOBAL, "print");
    LUA->PushString("Hello World");
    LUA->Call(1, 0);
} // Here main thread will be desynced, if it was synced
  // That means what LuaThreading::Lua will hold main thread until it destroyed


// Or we can force sync by passing boolean
LuaThreading::AcquireLua(true); // Will sync with main thread
LuaThreading::AcquireLua(false); // Won't sync

Reference

// lua_threading.hpp

namespace LuaThreading {
    // Global Thread ID to determine if we need to sync threads
    extern std::thread::id MainThreadID;

    class Lua {
    public:
        lua_State* state;

        Lua(); // Runs LuaThreading::NeedSync internally
        Lua(bool sync); // true if you need to sync with main thread

        ~Lua(); // Destructor will unlock main thread, if it was locked

        // Copying is prohibited
        Lua(const Lua& other) = delete;
        Lua& operator=(const Lua& other) = delete;

        // Move constructor is permitted, but not move statement
        Lua(Lua&& other) noexcept;
        Lua& operator=(Lua&& other) = delete;

        // You can use LuaThreading::Lua class as it was Garrysmod::Lua::ILuaBase*
        GarrysMod::Lua::ILuaBase* operator->() const;
    }

    // Returns true, if called in non main thread
    bool NeedSync();

    // Same as LuaThreading::Lua();
    Lua AcquireLua();
    Lua AcquireLua(bool sync);

    void Initialize(GarrysMod::Lua::ILuaBase* LUA);
    void Deinitialize(GarrysMod::Lua::ILuaBase* LUA);

    int Think(lua_State* L);
    int Think(GarrysMod::Lua::ILuaBase* LUA);
}

ToDo

  • Create joinable std::thread with context, so we can avoid deadlocks if thread was joined

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.