Skip to content

Commit

Permalink
t h r e a d s
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtleP committed Mar 27, 2024
1 parent f809e8a commit c5aa4f3
Show file tree
Hide file tree
Showing 32 changed files with 1,734 additions and 21 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,14 @@ source/modules/filesystem/wrap_File.cpp
source/modules/filesystem/wrap_FileData.cpp
source/modules/filesystem/wrap_Filesystem.cpp
source/modules/love/love.cpp
source/modules/system/wrap_System.cpp
source/modules/thread/Channel.cpp
source/modules/thread/LuaThread.cpp
source/modules/thread/Thread.cpp
source/modules/thread/Threadable.cpp
source/modules/thread/ThreadModule.cpp
source/modules/thread/wrap_Channel.cpp
source/modules/thread/wrap_LuaThread.cpp
source/modules/thread/wrap_Thread.cpp
source/modules/timer/wrap_Timer.cpp
)
3 changes: 3 additions & 0 deletions include/common/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ namespace love
#define E_DATA_PACK_OFFSET_FORMAT_PARAMS \
"The given byte offset and pack format parameters do not fit within the ByteData's size."
#define E_DATA_SIZE_MUST_BE_POSITIVE "Data size must be a positive number."
// Thread
#define E_CHANNEL_VARIANT_UNKNOWN "boolean, number, string, love type, or table expected."
#define E_THREAD_VARIANT_UNKNOWN "boolean, number, string, love type, or flat table expected."
} // namespace love
13 changes: 7 additions & 6 deletions include/common/luax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace love
const char* name;
Type* type;
std::span<const luaL_Reg> functions;
std::span<const luaL_Reg> platformFunctions;
std::span<const lua_CFunction> types;
};

Expand Down Expand Up @@ -220,15 +221,14 @@ namespace love
Variant luax_checkvariant(lua_State* L, int index, bool allowuserdata = true,
std::set<const void*>* tableSet = nullptr);

int luax_convobj(lua_State* L, int index, const char* module, const char* function);
int luax_assert_nilerror(lua_State* L, int index);

int luax_convobj(lua_State* L, const int indices[], int argc, const char* module,
const char* function);
int luax_getfunction(lua_State* L, const char* module, const char* name);

int luax_convobj(lua_State* L, const std::vector<int>& indices, const char* module,
const char* function);
int luax_convobj(lua_State* L, int index, const char* module, const char* function);

int luax_getfunction(lua_State* L, const char* module, const char* name);
int luax_convobj(lua_State* L, std::span<int> indices, const char* module,
const char* function);

// #endregion

Expand Down Expand Up @@ -277,6 +277,7 @@ namespace love
std::string expected = map.expected(name, value);
return luaL_error(L, "%s", expected.c_str());
}

// #endregion

// #region Other
Expand Down
11 changes: 11 additions & 0 deletions include/common/result.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace love
{
#define R_UNLESS(res_expr, throw_result) \
({ \
const auto _tmp_r_try_rc = (res_expr); \
if (res_expr < 0) \
return (throw_result); \
}) // namespace love
} // namespace love
3 changes: 2 additions & 1 deletion include/modules/event/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "events.hpp"

#include <mutex>
#include <queue>

namespace love
Expand All @@ -27,7 +28,7 @@ namespace love
Message* wait();

private:
std::mutex mutex;
std::recursive_mutex mutex;
std::queue<Message*> messages;

LOVE_Event event;
Expand Down
82 changes: 82 additions & 0 deletions include/modules/system/System.tcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#pragma once

#include "common/module.hpp"
#include "utility/map.hpp"

namespace love
{
template<class T>
class SystemBase : public Module
{
public:
enum PowerState
{
POWER_UNKNOWN,
POWER_BATTERY,
POWER_NO_BATTERY,
POWER_CHARGING,
POWER_CHARGED,
POWER_MAX_ENUM
};

enum NetworkState
{
NETWORK_UNKNOWN,
NETWORK_DISCONNECTED,
NETWORK_CONNECTED,
NETWORK_MAX_ENUM
};

SystemBase() : Module(M_SYSTEM, "love.system")
{}

virtual ~SystemBase()
{}

static const char* getOS()
{
return __OS__;
}

void setClipboardText(const std::string& text)
{
this->clipboard = text;
}

const std::string& getClipboardText() const
{
return this->clipboard;
}

struct FriendInfo
{
std::string username;
std::string friendCode;
};

struct SystemInfo
{
std::string version;
std::string model;
};

// clang-format off
STRINGMAP_DECLARE(powerStates, PowerState,
{ "unknown", POWER_UNKNOWN },
{ "battery", POWER_BATTERY },
{ "nobattery", POWER_NO_BATTERY },
{ "charging", POWER_CHARGING },
{ "charged", POWER_CHARGED }
);

STRINGMAP_DECLARE(networkStates, NetworkState,
{ "unknown", NETWORK_UNKNOWN },
{ "disconnected", NETWORK_DISCONNECTED },
{ "connected", NETWORK_CONNECTED }
);
// clang-format on

private:
std::string clipboard;
};
} // namespace love
35 changes: 35 additions & 0 deletions include/modules/system/wrap_System.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "common/luax.hpp"
#include "modules/system/System.hpp"

namespace Wrap_System
{
int getProcessorCount(lua_State* L);

int getPowerInfo(lua_State* L);

int getClipboardText(lua_State* L);

int setClipboardText(lua_State* L);

int vibrate(lua_State* L);

int openURL(lua_State* L);

int hasBackgroundMusic(lua_State* L);

int getPreferredLocales(lua_State* L);

int getNetworkInfo(lua_State* L);

int getInfo(lua_State* L);

int getOS(lua_State* L);

int getPlayCoins(lua_State* L);

int setPlayCoins(lua_State* L);

int open(lua_State* L);
} // namespace Wrap_System
61 changes: 61 additions & 0 deletions include/modules/thread/Channel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include "common/Object.hpp"
#include "common/Variant.hpp"

#include <condition_variable>
#include <mutex>

#include <queue>

namespace love
{
class Channel : public Object
{
public:
static Type type;

Channel();

virtual ~Channel()
{}

uint64_t push(const Variant& value);

bool supply(const Variant& value);

bool supply(const Variant& value, double timeout);

bool pop(Variant* value);

bool demand(Variant* value);

bool demand(Variant* value, double timeout);

bool peek(Variant* value);

bool hasRead(uint64_t id);

int getCount();

void clear();

void lockMutex();

void unlockMutex();

std::recursive_mutex& getMutex()
{
return this->mutex;
}

private:
uint64_t sent;
uint64_t received;

std::recursive_mutex mutex;
std::condition_variable_any condvar;

std::queue<Variant> queue;
};
} // namespace love
45 changes: 45 additions & 0 deletions include/modules/thread/LuaThread.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "common/Data.hpp"
#include "common/Object.hpp"
#include "common/StrongRef.hpp"
#include "common/Variant.hpp"

#include "modules/thread/Threadable.hpp"

#include <vector>

namespace love
{
class LuaThread : public Threadable
{
public:
static Type type;

LuaThread(const std::string& name, Data* code);

virtual ~LuaThread()
{}

void run() override;

const std::string& getError() const;

bool hasError() const
{
return this->_hasError;
}

bool start(const std::vector<Variant>& args);

private:
void onError();

StrongRef<Data> code;

std::string name;
std::string error;

bool _hasError;

std::vector<Variant> args;
};
} // namespace love
34 changes: 34 additions & 0 deletions include/modules/thread/Thread.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "common/Object.hpp"

#include <mutex>
#include <thread>

namespace love
{
class Threadable;

class Thread
{
public:
Thread(Threadable* threadable);

virtual ~Thread();

bool start();

void wait();

bool isRunning();

private:
static int runner(void* data);

Threadable* threadable;
std::atomic<bool> running;

std::thread thread;
std::mutex mutex;
};
} // namespace love
34 changes: 34 additions & 0 deletions include/modules/thread/ThreadModule.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "common/Data.hpp"
#include "common/Module.hpp"
#include "utility/map.hpp"

#include "modules/thread/Channel.hpp"
#include "modules/thread/LuaThread.hpp"
#include "modules/thread/Thread.hpp"

#include <map>
#include <string>

namespace love
{
class ThreadModule : public Module
{
public:
ThreadModule();

virtual ~ThreadModule()
{}

LuaThread* newThread(const std::string& name, Data* data) const;

Channel* newChannel() const;

Channel* getChannel(const std::string& name);

private:
std::map<std::string, StrongRef<Channel>> channels;
std::recursive_mutex mutex;
};
} // namespace love
Loading

0 comments on commit c5aa4f3

Please sign in to comment.