Add docker file
This commit is contained in:
@ -134,9 +134,14 @@ install(EXPORT TessesFrameworkTargets
|
|||||||
include(CMakePackageConfigHelpers)
|
include(CMakePackageConfigHelpers)
|
||||||
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/TessesFrameworkConfig.cmake"
|
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/TessesFrameworkConfig.cmake"
|
||||||
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/TessesFramework)
|
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/TessesFramework)
|
||||||
|
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/TessesFrameworkFeatures.h.in "${CMAKE_CURRENT_BINARY_DIR}/TessesFrameworkFeatures.h"
|
||||||
|
INSTALL_DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/TessesFramework/TessesFrameworkFeatures.h)
|
||||||
|
|
||||||
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/TessesFrameworkConfig.cmake"
|
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/TessesFrameworkConfig.cmake"
|
||||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/TessesFramework)
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/TessesFramework)
|
||||||
|
|
||||||
|
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/TessesFrameworkFeatures.h"
|
||||||
|
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/TessesFramework)
|
||||||
endif()
|
endif()
|
||||||
if(TESSESFRAMEWORK_ENABLE_EXAMPLES)
|
if(TESSESFRAMEWORK_ENABLE_EXAMPLES)
|
||||||
add_executable(copyfile examples/copyfile.cpp)
|
add_executable(copyfile examples/copyfile.cpp)
|
||||||
@ -162,4 +167,4 @@ add_executable(tfileserver apps/tfileserver.cpp)
|
|||||||
target_link_libraries(tfileserver PUBLIC tessesframework)
|
target_link_libraries(tfileserver PUBLIC tessesframework)
|
||||||
install(TARGETS tfileserver DESTINATION bin)
|
install(TARGETS tfileserver DESTINATION bin)
|
||||||
|
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
5
Dockerfile
Normal file
5
Dockerfile
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
FROM ubuntu:noble
|
||||||
|
|
||||||
|
RUN apt update && apt install -y cmake g++ gcc libmbedtls-dev build-essential
|
||||||
|
|
||||||
|
RUN mkdir build && cd build && cmake -S .. -B . && make -j12 && make install
|
||||||
8
TessesFrameworkFeatures.h.in
Normal file
8
TessesFrameworkFeatures.h.in
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
#define TESSES_FRAMEWORK_FLAG_OFF 0
|
||||||
|
#define TESSES_FRAMEWORK_FLAG_ON 1
|
||||||
|
#if TESSES_FRAMEWORK_FLAG_@TESSESFRAMEWORK_ENABLE_MBED@
|
||||||
|
#define TESSESFRAMEWORK_ENABLE_MBED
|
||||||
|
#endif
|
||||||
|
#undef TESSES_FRAMEWORK_FLAG_OFF
|
||||||
|
#undef TESSES_FRAMEWORK_FLAG_ON
|
||||||
@ -11,19 +11,23 @@ namespace Tesses::Framework::Filesystem
|
|||||||
public:
|
public:
|
||||||
static VFSPath RelativeCurrentDirectory();
|
static VFSPath RelativeCurrentDirectory();
|
||||||
bool relative;
|
bool relative;
|
||||||
static std::vector<std::string> SplitPath(std::string path,bool native=false);
|
static std::vector<std::string> SplitPath(std::string path);
|
||||||
std::vector<std::string> path;
|
std::vector<std::string> path;
|
||||||
VFSPath();
|
VFSPath();
|
||||||
VFSPath(std::vector<std::string> path);
|
VFSPath(std::vector<std::string> path);
|
||||||
VFSPath(std::string path);
|
VFSPath(std::string path);
|
||||||
VFSPath(VFSPath p, std::string subent);
|
VFSPath(VFSPath p, std::string subent);
|
||||||
VFSPath(VFSPath p, VFSPath p2);
|
VFSPath(VFSPath p, VFSPath p2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
VFSPath GetParent();
|
VFSPath GetParent();
|
||||||
VFSPath CollapseRelativeParents();
|
VFSPath CollapseRelativeParents();
|
||||||
std::string GetFileName();
|
std::string GetFileName();
|
||||||
|
bool HasExtension();
|
||||||
|
std::string GetExtension();
|
||||||
|
void ChangeExtension(std::string ext);
|
||||||
|
void RemoveExtension();
|
||||||
std::string ToString();
|
std::string ToString();
|
||||||
};
|
};
|
||||||
VFSPath operator/(VFSPath p, VFSPath p2);
|
VFSPath operator/(VFSPath p, VFSPath p2);
|
||||||
@ -32,7 +36,12 @@ namespace Tesses::Framework::Filesystem
|
|||||||
VFSPath operator+(VFSPath p, VFSPath p2);
|
VFSPath operator+(VFSPath p, VFSPath p2);
|
||||||
VFSPath operator+(VFSPath p, std::string p2);
|
VFSPath operator+(VFSPath p, std::string p2);
|
||||||
VFSPath operator+(std::string p, VFSPath p2);
|
VFSPath operator+(std::string p, VFSPath p2);
|
||||||
|
bool operator==(VFSPath p,VFSPath p2);
|
||||||
|
bool operator!=(VFSPath p,VFSPath p2);
|
||||||
|
bool operator==(std::string p,VFSPath p2);
|
||||||
|
bool operator!=(std::string p,VFSPath p2);
|
||||||
|
bool operator==(VFSPath p,std::string p2);
|
||||||
|
bool operator!=(VFSPath p,std::string p2);
|
||||||
class VFSPathEnumeratorData {
|
class VFSPathEnumeratorData {
|
||||||
public:
|
public:
|
||||||
VFSPathEnumeratorData(std::function<bool(VFSPath&)> moveNext, std::function<void()> destroy)
|
VFSPathEnumeratorData(std::function<bool(VFSPath&)> moveNext, std::function<void()> destroy)
|
||||||
|
|||||||
51
include/TessesFramework/Lazy.hpp
Normal file
51
include/TessesFramework/Lazy.hpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Common.hpp"
|
||||||
|
#include "Threading/Mutex.hpp"
|
||||||
|
#include <functional>
|
||||||
|
namespace Tesses::Framework {
|
||||||
|
template<typename T>
|
||||||
|
class Lazy {
|
||||||
|
Threading::Mutex mtx;
|
||||||
|
T value;
|
||||||
|
bool hasInit=false;
|
||||||
|
std::function<T()> init;
|
||||||
|
std::function<void(T)> free;
|
||||||
|
public:
|
||||||
|
Lazy(std::function<T()> init, std::function<void(T)> free)
|
||||||
|
{
|
||||||
|
this->init = init;
|
||||||
|
this->free = free;
|
||||||
|
}
|
||||||
|
Lazy(std::function<T()> init) : Lazy(init, [](T item)->void {})
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
bool HasInit()
|
||||||
|
{
|
||||||
|
mtx.Lock();
|
||||||
|
bool hI = this->hasInit;
|
||||||
|
mtx.Unlock();
|
||||||
|
return hI;
|
||||||
|
}
|
||||||
|
T& GetValue()
|
||||||
|
{
|
||||||
|
mtx.Lock();
|
||||||
|
if(hasInit)
|
||||||
|
{
|
||||||
|
mtx.Unlock();
|
||||||
|
return this->value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this->value = this->init();
|
||||||
|
mtx.Unlock();
|
||||||
|
return this->value;
|
||||||
|
}
|
||||||
|
mtx.Unlock();
|
||||||
|
}
|
||||||
|
~Lazy()
|
||||||
|
{
|
||||||
|
if(hasInit) this->free(this->value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -17,4 +17,5 @@
|
|||||||
#include "Filesystem/NullFilesystem.hpp"
|
#include "Filesystem/NullFilesystem.hpp"
|
||||||
#include "Filesystem/MountableFilesystem.hpp"
|
#include "Filesystem/MountableFilesystem.hpp"
|
||||||
#include "Filesystem/MemoryFilesystem.hpp"
|
#include "Filesystem/MemoryFilesystem.hpp"
|
||||||
#include "Crypto/ClientTLSStream.hpp"
|
#include "Crypto/ClientTLSStream.hpp"
|
||||||
|
#include "Lazy.hpp"
|
||||||
@ -4,7 +4,7 @@
|
|||||||
#elif defined(GEKKO)
|
#elif defined(GEKKO)
|
||||||
#include <ogc/mutex.h>
|
#include <ogc/mutex.h>
|
||||||
#else
|
#else
|
||||||
#include <threads.h>
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
namespace Tesses::Framework::Threading
|
namespace Tesses::Framework::Threading
|
||||||
{
|
{
|
||||||
@ -14,7 +14,8 @@ namespace Tesses::Framework::Threading
|
|||||||
#elif defined(GEKKO)
|
#elif defined(GEKKO)
|
||||||
mutex_t mtx;
|
mutex_t mtx;
|
||||||
#else
|
#else
|
||||||
mtx_t mtx;
|
pthread_mutex_t mtx;
|
||||||
|
pthread_mutexattr_t attr;
|
||||||
#endif
|
#endif
|
||||||
public:
|
public:
|
||||||
Mutex();
|
Mutex();
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
#elif defined(GEKKO)
|
#elif defined(GEKKO)
|
||||||
#include <ogc/lwp.h>
|
#include <ogc/lwp.h>
|
||||||
#else
|
#else
|
||||||
#include <threads.h>
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
namespace Tesses::Framework::Threading
|
namespace Tesses::Framework::Threading
|
||||||
@ -22,8 +22,8 @@ namespace Tesses::Framework::Threading
|
|||||||
lwp_t thrd;
|
lwp_t thrd;
|
||||||
static void* cb(void* ptr);
|
static void* cb(void* ptr);
|
||||||
#else
|
#else
|
||||||
thrd_t thrd;
|
pthread_t thrd;
|
||||||
static int cb(void* ptr);
|
static void* cb(void* ptr);
|
||||||
#endif
|
#endif
|
||||||
std::function<void()> fn;
|
std::function<void()> fn;
|
||||||
|
|
||||||
|
|||||||
@ -117,7 +117,7 @@ namespace Tesses::Framework::Filesystem
|
|||||||
VFSPath LocalFilesystem::SystemToVFSPath(std::string path)
|
VFSPath LocalFilesystem::SystemToVFSPath(std::string path)
|
||||||
{
|
{
|
||||||
VFSPath p;
|
VFSPath p;
|
||||||
p.path = VFSPath::SplitPath(path,true);
|
p.path = VFSPath::SplitPath(path);
|
||||||
p.relative=true;
|
p.relative=true;
|
||||||
if(!path.empty())
|
if(!path.empty())
|
||||||
{
|
{
|
||||||
|
|||||||
@ -153,6 +153,39 @@ namespace Tesses::Framework::Filesystem
|
|||||||
{
|
{
|
||||||
return VFSPath(p) + p2;
|
return VFSPath(p) + p2;
|
||||||
}
|
}
|
||||||
|
bool operator==(VFSPath p,VFSPath p2)
|
||||||
|
{
|
||||||
|
if(p.relative != p2.relative) return false;
|
||||||
|
if(p.path.size() != p2.path.size()) return false;
|
||||||
|
for(size_t i = 0; i < p.path.size(); i++)
|
||||||
|
if(p.path[i] != p2.path[i]) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool operator!=(VFSPath p,VFSPath p2)
|
||||||
|
{
|
||||||
|
if(p.relative != p2.relative) return true;
|
||||||
|
if(p.path.size() != p2.path.size()) return true;
|
||||||
|
for(size_t i = 0; i < p.path.size(); i++)
|
||||||
|
if(p.path[i] != p2.path[i]) return true;
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
bool operator==(std::string p,VFSPath p2)
|
||||||
|
{
|
||||||
|
return VFSPath(p) == p2;
|
||||||
|
}
|
||||||
|
bool operator!=(std::string p,VFSPath p2)
|
||||||
|
{
|
||||||
|
return VFSPath(p) != p2;
|
||||||
|
}
|
||||||
|
bool operator==(VFSPath p,std::string p2)
|
||||||
|
{
|
||||||
|
return p == VFSPath(p2);
|
||||||
|
}
|
||||||
|
bool operator!=(VFSPath p,std::string p2)
|
||||||
|
{
|
||||||
|
return p != VFSPath(p2);
|
||||||
|
}
|
||||||
VFSPath VFS::ReadLink(VFSPath path)
|
VFSPath VFS::ReadLink(VFSPath path)
|
||||||
{
|
{
|
||||||
return VFSPath("/");
|
return VFSPath("/");
|
||||||
@ -191,7 +224,7 @@ namespace Tesses::Framework::Filesystem
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> VFSPath::SplitPath(std::string path,bool nativePath)
|
std::vector<std::string> VFSPath::SplitPath(std::string path)
|
||||||
{
|
{
|
||||||
std::vector<std::string> parts;
|
std::vector<std::string> parts;
|
||||||
std::string curPath = {};
|
std::string curPath = {};
|
||||||
@ -206,7 +239,7 @@ namespace Tesses::Framework::Filesystem
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#if defined(WIN32)
|
#if defined(WIN32)
|
||||||
else if(c == '\\' && nativePath)
|
else if(c == '\\')
|
||||||
{
|
{
|
||||||
if(!curPath.empty())
|
if(!curPath.empty())
|
||||||
{
|
{
|
||||||
@ -236,6 +269,47 @@ namespace Tesses::Framework::Filesystem
|
|||||||
{
|
{
|
||||||
this->path = p;
|
this->path = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool VFSPath::HasExtension()
|
||||||
|
{
|
||||||
|
if(this->path.empty()) return false;
|
||||||
|
auto& str = this->path.back();
|
||||||
|
auto index = str.find_last_of('.');
|
||||||
|
if(index == std::string::npos) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
std::string VFSPath::GetExtension()
|
||||||
|
{
|
||||||
|
if(this->path.empty()) return {};
|
||||||
|
auto& str = this->path.back();
|
||||||
|
auto index = str.find_last_of('.');
|
||||||
|
if(index == std::string::npos) return {};
|
||||||
|
return str.substr(index);
|
||||||
|
}
|
||||||
|
void VFSPath::ChangeExtension(std::string ext)
|
||||||
|
{
|
||||||
|
if(this->path.empty()) return;
|
||||||
|
auto& str = this->path.back();
|
||||||
|
auto index = str.find_last_of('.');
|
||||||
|
if(index != std::string::npos)
|
||||||
|
{
|
||||||
|
str = str.substr(0,index);
|
||||||
|
}
|
||||||
|
if(ext.empty()) return;
|
||||||
|
if(ext[0] != '.')
|
||||||
|
{
|
||||||
|
str += '.' + ext;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
str += ext;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void VFSPath::RemoveExtension()
|
||||||
|
{
|
||||||
|
ChangeExtension({});
|
||||||
|
}
|
||||||
|
|
||||||
VFSPath::VFSPath(std::string str)
|
VFSPath::VFSPath(std::string str)
|
||||||
{
|
{
|
||||||
this->path = SplitPath(str);
|
this->path = SplitPath(str);
|
||||||
@ -247,7 +321,7 @@ namespace Tesses::Framework::Filesystem
|
|||||||
{
|
{
|
||||||
auto firstPartPath = this->path.front();
|
auto firstPartPath = this->path.front();
|
||||||
|
|
||||||
if(!firstPartPath.empty() && firstPartPath.back() == '/') this->relative=false;
|
if(!firstPartPath.empty() && firstPartPath.back() == ':') this->relative=false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -265,6 +339,8 @@ namespace Tesses::Framework::Filesystem
|
|||||||
{
|
{
|
||||||
std::vector<std::string> paths;
|
std::vector<std::string> paths;
|
||||||
if(this->path.empty()) return VFSPath();
|
if(this->path.empty()) return VFSPath();
|
||||||
|
if(!this->relative && this->path.size() == 1 && !this->path[0].empty() && this->path[0].back() == ':') return *this;
|
||||||
|
|
||||||
paths.insert(paths.begin(), this->path.begin(), this->path.end()-1);
|
paths.insert(paths.begin(), this->path.begin(), this->path.end()-1);
|
||||||
auto res= VFSPath(paths);
|
auto res= VFSPath(paths);
|
||||||
res.relative = this->relative;
|
res.relative = this->relative;
|
||||||
@ -280,6 +356,7 @@ namespace Tesses::Framework::Filesystem
|
|||||||
std::string VFSPath::ToString()
|
std::string VFSPath::ToString()
|
||||||
{
|
{
|
||||||
if(this->path.empty() && !this->relative) return "/";
|
if(this->path.empty() && !this->relative) return "/";
|
||||||
|
if(!this->relative && this->path.size() == 1 && !this->path[0].empty() && this->path[0].back() == ':') return this->path[0] + "/";
|
||||||
bool first=true;
|
bool first=true;
|
||||||
std::string p = {};
|
std::string p = {};
|
||||||
for(auto item : this->path)
|
for(auto item : this->path)
|
||||||
|
|||||||
@ -94,6 +94,31 @@ namespace Tesses::Framework::Http
|
|||||||
{
|
{
|
||||||
if(this->queryParams.kvp.empty()) return this->originalPath;
|
if(this->queryParams.kvp.empty()) return this->originalPath;
|
||||||
return this->originalPath + "?" + HttpUtils::QueryParamsEncode(this->queryParams);
|
return this->originalPath + "?" + HttpUtils::QueryParamsEncode(this->queryParams);
|
||||||
|
}
|
||||||
|
void ServerContext::ReadStream(Stream* strm)
|
||||||
|
{
|
||||||
|
if(strm == nullptr) return;
|
||||||
|
auto strm2 = this->OpenRequestStream();
|
||||||
|
if(strm2 != nullptr)
|
||||||
|
{
|
||||||
|
strm2->CopyTo(strm);
|
||||||
|
delete strm2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void ServerContext::ReadStream(Stream& strm)
|
||||||
|
{
|
||||||
|
ReadStream(&strm);
|
||||||
|
}
|
||||||
|
std::string ServerContext::ReadString()
|
||||||
|
{
|
||||||
|
if(strm == nullptr) return {};
|
||||||
|
auto strm2 = this->OpenRequestStream();
|
||||||
|
if(strm2 != nullptr)
|
||||||
|
{
|
||||||
|
TextStreams::StreamReader reader(strm2,true);
|
||||||
|
return reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
bool ServerContext::NeedToParseFormData()
|
bool ServerContext::NeedToParseFormData()
|
||||||
{
|
{
|
||||||
@ -628,7 +653,7 @@ namespace Tesses::Framework::Http
|
|||||||
uint8_t* buffer = new uint8_t[len];
|
uint8_t* buffer = new uint8_t[len];
|
||||||
len = bStrm.ReadBlock(buffer,len);
|
len = bStrm.ReadBlock(buffer,len);
|
||||||
std::string query((const char*)buffer,len);
|
std::string query((const char*)buffer,len);
|
||||||
delete buffer;
|
delete[] buffer;
|
||||||
HttpUtils::QueryParamsDecode(ctx.queryParams, query);
|
HttpUtils::QueryParamsDecode(ctx.queryParams, query);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -658,4 +683,4 @@ namespace Tesses::Framework::Http
|
|||||||
if(bStrm.EndOfStream()) return;
|
if(bStrm.EndOfStream()) return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,7 +32,7 @@ extern "C" {
|
|||||||
#if defined(GEKKO)
|
#if defined(GEKKO)
|
||||||
|
|
||||||
extern "C" uint32_t if_config( char *local_ip, char *netmask, char *gateway,bool use_dhcp, int max_retries);
|
extern "C" uint32_t if_config( char *local_ip, char *netmask, char *gateway,bool use_dhcp, int max_retries);
|
||||||
#elif !defined(_WIN32)
|
#elif !defined(_WIN32) && !defined(__ANDROID__)
|
||||||
#include <ifaddrs.h>
|
#include <ifaddrs.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ namespace Tesses::Framework::Streams {
|
|||||||
char gateway[16];
|
char gateway[16];
|
||||||
if_config(localIp,netmask, gateway, true, 1);
|
if_config(localIp,netmask, gateway, true, 1);
|
||||||
ipConfig.push_back(std::pair<std::string,std::string>("net",localIp));
|
ipConfig.push_back(std::pair<std::string,std::string>("net",localIp));
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32) || defined(__ANDROID__)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
struct ifaddrs *ifAddrStruct = NULL;
|
struct ifaddrs *ifAddrStruct = NULL;
|
||||||
@ -503,7 +503,10 @@ namespace Tesses::Framework::Streams {
|
|||||||
if(!this->success) return 0;
|
if(!this->success) return 0;
|
||||||
|
|
||||||
ssize_t sz2 = NETWORK_SEND(this->sock,(const char*)buff,sz, 0);
|
ssize_t sz2 = NETWORK_SEND(this->sock,(const char*)buff,sz, 0);
|
||||||
if(sz2 < 0) return 0;
|
if(sz2 <= 0) {
|
||||||
|
this->endOfStream=true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
return (size_t)sz;
|
return (size_t)sz;
|
||||||
}
|
}
|
||||||
@ -550,4 +553,4 @@ namespace Tesses::Framework::Streams {
|
|||||||
if(this->owns && this->success)
|
if(this->owns && this->success)
|
||||||
NETWORK_CLOSE(this->sock);
|
NETWORK_CLOSE(this->sock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#include "TessesFramework/Threading/Mutex.hpp"
|
#include "TessesFramework/Threading/Mutex.hpp"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
namespace Tesses::Framework::Threading
|
namespace Tesses::Framework::Threading
|
||||||
{
|
{
|
||||||
Mutex::Mutex()
|
Mutex::Mutex()
|
||||||
@ -10,9 +11,10 @@ namespace Tesses::Framework::Threading
|
|||||||
mtx = LWP_MUTEX_NULL;
|
mtx = LWP_MUTEX_NULL;
|
||||||
LWP_MutexInit(&mtx, true);
|
LWP_MutexInit(&mtx, true);
|
||||||
#else
|
#else
|
||||||
memset(&mtx, 0, sizeof(mtx_t));
|
pthread_mutexattr_init(&attr);
|
||||||
mtx_init(&mtx, mtx_recursive);
|
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
|
||||||
|
pthread_mutex_init(&mtx,&attr);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
void Mutex::Lock()
|
void Mutex::Lock()
|
||||||
@ -22,7 +24,7 @@ namespace Tesses::Framework::Threading
|
|||||||
#elif defined(GEKKO)
|
#elif defined(GEKKO)
|
||||||
LWP_MutexLock(mtx);
|
LWP_MutexLock(mtx);
|
||||||
#else
|
#else
|
||||||
mtx_lock(&mtx);
|
pthread_mutex_lock(&mtx);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
void Mutex::Unlock()
|
void Mutex::Unlock()
|
||||||
@ -32,7 +34,7 @@ namespace Tesses::Framework::Threading
|
|||||||
#elif defined(GEKKO)
|
#elif defined(GEKKO)
|
||||||
LWP_MutexUnlock(mtx);
|
LWP_MutexUnlock(mtx);
|
||||||
#else
|
#else
|
||||||
mtx_unlock(&mtx);
|
pthread_mutex_unlock(&mtx);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
bool Mutex::TryLock()
|
bool Mutex::TryLock()
|
||||||
@ -42,7 +44,7 @@ namespace Tesses::Framework::Threading
|
|||||||
#elif defined(GEKKO)
|
#elif defined(GEKKO)
|
||||||
return LWP_MutexTryLock(mtx) == 0;
|
return LWP_MutexTryLock(mtx) == 0;
|
||||||
#else
|
#else
|
||||||
return mtx_trylock(&mtx) == thrd_success;
|
return pthread_mutex_trylock(&mtx) == 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
Mutex::~Mutex()
|
Mutex::~Mutex()
|
||||||
@ -52,8 +54,9 @@ namespace Tesses::Framework::Threading
|
|||||||
#elif defined(GEKKO)
|
#elif defined(GEKKO)
|
||||||
LWP_MutexDestroy(mtx);
|
LWP_MutexDestroy(mtx);
|
||||||
#else
|
#else
|
||||||
mtx_destroy(&mtx);
|
pthread_mutex_destroy(&mtx);
|
||||||
|
pthread_mutexattr_destroy(&attr);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -18,22 +18,17 @@ namespace Tesses::Framework::Threading
|
|||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#if defined(GEKKO)
|
|
||||||
void* Thread::cb(void* data)
|
void* Thread::cb(void* data)
|
||||||
#else
|
{
|
||||||
int Thread::cb(void* data)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
auto thrd = static_cast<Thread*>(data);
|
auto thrd = static_cast<Thread*>(data);
|
||||||
|
|
||||||
auto fn = thrd->fn;
|
auto fn = thrd->fn;
|
||||||
thrd->hasInvoked=true;
|
thrd->hasInvoked=true;
|
||||||
fn();
|
fn();
|
||||||
#if defined(GEKKO)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
#else
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
Thread::Thread(std::function<void()> fn)
|
Thread::Thread(std::function<void()> fn)
|
||||||
@ -43,10 +38,10 @@ namespace Tesses::Framework::Threading
|
|||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
this->thrd = CreateThread(NULL,0,cb,static_cast<LPVOID>(this), 0, &this->thrdId);
|
this->thrd = CreateThread(NULL,0,cb,static_cast<LPVOID>(this), 0, &this->thrdId);
|
||||||
#elif defined(GEKKO)
|
#elif defined(GEKKO)
|
||||||
thrd = LWP_THREAD_NULL;
|
auto res = LWP_CreateThread(&this->thrd, cb, static_cast<void*>(this), nullptr,12000, 98);
|
||||||
LWP_CreateThread(&thrd, cb, static_cast<void*>(this), NULL, 12000, LWP_PRIO_HIGHEST);
|
|
||||||
#else
|
#else
|
||||||
thrd_create(&thrd, cb, static_cast<void*>(this));
|
pthread_create(&thrd,NULL,cb,static_cast<void*>(this));
|
||||||
|
//thrd_create(&thrd, cb, static_cast<void*>(this));
|
||||||
#endif
|
#endif
|
||||||
while(!this->hasInvoked);
|
while(!this->hasInvoked);
|
||||||
}
|
}
|
||||||
@ -56,7 +51,7 @@ namespace Tesses::Framework::Threading
|
|||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
CloseHandle(thrd);
|
CloseHandle(thrd);
|
||||||
#else
|
#else
|
||||||
thrd_detach(thrd);
|
pthread_detach(thrd);
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -69,9 +64,8 @@ namespace Tesses::Framework::Threading
|
|||||||
void* res;
|
void* res;
|
||||||
LWP_JoinThread(thrd,&res);
|
LWP_JoinThread(thrd,&res);
|
||||||
#else
|
#else
|
||||||
int res;
|
pthread_join(thrd,NULL);
|
||||||
thrd_join(thrd,&res);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user