ftpiiu_plugin/source/3ds/platform.cpp

633 lines
15 KiB
C++
Raw Normal View History

2020-04-05 21:16:16 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "platform.h"
#include "fs.h"
2020-04-23 00:15:23 +02:00
#include "ftpServer.h"
2020-04-05 21:16:16 +02:00
#include "log.h"
#include "imgui_citro3d.h"
#include "imgui_ctru.h"
#include "imgui.h"
#include <citro3d.h>
#include <tex3ds.h>
2020-04-17 22:32:39 +02:00
#ifndef CLASSIC
2020-04-05 21:16:16 +02:00
#include "gfx.h"
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
2020-04-17 22:32:39 +02:00
#include <arpa/inet.h>
#include <malloc.h>
#include <atomic>
2020-04-05 21:16:16 +02:00
#include <cassert>
#include <chrono>
2020-04-17 22:32:39 +02:00
#include <cstring>
2020-04-05 21:16:16 +02:00
#include <ctime>
#include <mutex>
2020-04-05 21:16:16 +02:00
2020-04-17 22:32:39 +02:00
#ifdef CLASSIC
PrintConsole g_statusConsole;
PrintConsole g_logConsole;
PrintConsole g_sessionConsole;
#endif
2020-04-05 21:16:16 +02:00
namespace
{
2020-04-06 07:36:03 +02:00
/// \brief Thread stack size
constexpr auto STACK_SIZE = 0x8000;
/// \brief soc:u buffer alignment
constexpr auto SOCU_ALIGN = 0x1000;
/// \brief soc:u buffer size
2020-04-05 21:16:16 +02:00
constexpr auto SOCU_BUFFERSIZE = 0x100000;
static_assert (SOCU_BUFFERSIZE % SOCU_ALIGN == 0);
2020-04-06 07:36:03 +02:00
/// \brief Whether soc:u is active
std::atomic<bool> s_socuActive = false;
2020-04-06 07:36:03 +02:00
/// \brief soc:u buffer
2020-04-05 21:16:16 +02:00
u32 *s_socuBuffer = nullptr;
/// \brief ac:u fence
platform::Mutex s_acuFence;
2020-04-05 21:16:16 +02:00
2020-04-28 02:57:09 +02:00
/// \brief Whether to power backlight
bool s_backlight = true;
/// \brief APT hook cookie
aptHookCookie s_aptHookCookie;
2020-04-17 22:32:39 +02:00
#ifdef CLASSIC
2020-05-08 06:30:23 +02:00
/// \brief Host address
2020-04-17 22:32:39 +02:00
in_addr_t s_addr = 0;
#else
/// \brief Clear color
constexpr auto CLEAR_COLOR = 0x204B7AFF;
/// \brief Screen width
constexpr auto SCREEN_WIDTH = 400.0f;
/// \brief Screen height
constexpr auto SCREEN_HEIGHT = 480.0f;
/// \brief Whether to use anti-aliasing
#define ANTI_ALIAS 1
#if ANTI_ALIAS
/// \brief Display transfer scaling
constexpr auto TRANSFER_SCALING = GX_TRANSFER_SCALE_XY;
/// \brief Framebuffer scale
constexpr auto FB_SCALE = 2.0f;
#else
/// \brief Display transfer scaling
constexpr auto TRANSFER_SCALING = GX_TRANSFER_SCALE_NO;
/// \brief Framebuffer scale
constexpr auto FB_SCALE = 1.0f;
#endif
/// \brief Framebuffer width
constexpr auto FB_WIDTH = SCREEN_WIDTH * FB_SCALE;
/// \brief Framebuffer height
constexpr auto FB_HEIGHT = SCREEN_HEIGHT * FB_SCALE;
/// \brief Display transfer flags
constexpr auto DISPLAY_TRANSFER_FLAGS =
GX_TRANSFER_FLIP_VERT (0) | GX_TRANSFER_OUT_TILED (0) | GX_TRANSFER_RAW_COPY (0) |
GX_TRANSFER_IN_FORMAT (GX_TRANSFER_FMT_RGBA8) | GX_TRANSFER_OUT_FORMAT (GX_TRANSFER_FMT_RGB8) |
GX_TRANSFER_SCALING (TRANSFER_SCALING);
/// \brief Top screen render target
C3D_RenderTarget *s_top = nullptr;
/// \brief Bottom screen render target
C3D_RenderTarget *s_bottom = nullptr;
2020-04-06 07:36:03 +02:00
/// \brief Texture atlas
2020-04-05 21:16:16 +02:00
C3D_Tex s_gfxTexture;
2020-04-06 07:36:03 +02:00
/// \brief Texture atlas metadata
2020-04-05 21:16:16 +02:00
Tex3DS_Texture s_gfxT3x;
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
2020-04-28 02:57:09 +02:00
/// \brief Enable backlight
/// \param enable_ Whether to enable backligh
void enableBacklight (bool const enable_)
{
if (R_FAILED (gspLcdInit ()))
return;
(enable_ ? GSPLCD_PowerOnBacklight : GSPLCD_PowerOffBacklight) (GSPLCD_SCREEN_BOTH);
gspLcdExit ();
}
/// \brief Handle APT cookie
/// \param type_ Hook type
/// \param param_ User param
void handleAPTHook (APT_HookType const type_, void *const param_)
{
switch (type_)
{
case APTHOOK_ONSUSPEND:
case APTHOOK_ONSLEEP:
// turn on backlight, or you can't see the home menu!
if (!s_backlight)
enableBacklight (true);
break;
case APTHOOK_ONRESTORE:
case APTHOOK_ONWAKEUP:
// restore backlight setting
enableBacklight (s_backlight);
break;
default:
break;
}
}
/// \brief Get network visibility
bool getNetworkVisibility ()
{
// serialize ac:u access from multiple threads
auto lock = std::scoped_lock (s_acuFence);
// get wifi status
std::uint32_t wifi = 0;
if (R_FAILED (ACU_GetWifiStatus (&wifi)) || !wifi)
2020-04-17 22:32:39 +02:00
{
#ifdef CLASSIC
s_addr = 0;
#endif
return false;
2020-04-17 22:32:39 +02:00
}
#ifdef CLASSIC
if (!s_addr)
s_addr = gethostid ();
if (s_addr == INADDR_BROADCAST)
s_addr = 0;
#endif
return true;
}
2020-04-06 07:36:03 +02:00
/// \brief Start network
2020-04-05 21:16:16 +02:00
void startNetwork ()
{
2020-04-06 07:36:03 +02:00
// check if already active
2020-04-05 21:16:16 +02:00
if (s_socuActive)
return;
if (!getNetworkVisibility ())
2020-04-05 21:16:16 +02:00
return;
2020-04-06 07:36:03 +02:00
// allocate soc:u buffer
2020-04-05 21:16:16 +02:00
if (!s_socuBuffer)
s_socuBuffer = static_cast<u32 *> (::memalign (SOCU_ALIGN, SOCU_BUFFERSIZE));
if (!s_socuBuffer)
return;
2020-04-06 07:36:03 +02:00
// initialize soc:u service
2020-04-05 21:16:16 +02:00
if (R_FAILED (socInit (s_socuBuffer, SOCU_BUFFERSIZE)))
return;
s_socuActive = true;
2020-04-10 04:21:25 +02:00
info ("Wifi connected\n");
2020-04-05 21:16:16 +02:00
}
2020-04-06 07:36:03 +02:00
/// \brief Draw citro3d logo
2020-04-05 21:16:16 +02:00
void drawLogo ()
{
2020-04-17 22:32:39 +02:00
#ifndef CLASSIC
2020-04-06 07:36:03 +02:00
// get citro3d logo subtexture
2020-04-05 21:16:16 +02:00
auto subTex = Tex3DS_GetSubTexture (s_gfxT3x, gfx_c3dlogo_idx);
2020-04-06 07:36:03 +02:00
// get framebuffer metrics
auto const &io = ImGui::GetIO ();
2020-04-05 21:16:16 +02:00
auto const screenWidth = io.DisplaySize.x;
auto const screenHeight = io.DisplaySize.y;
2020-04-07 03:24:06 +02:00
auto const logoWidth = subTex->width;
auto const logoHeight = subTex->height;
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// calculate top screen coords
auto const x1 = (screenWidth - logoWidth) * 0.5f;
2020-04-05 21:16:16 +02:00
auto const x2 = x1 + logoWidth;
auto const y1 = (screenHeight * 0.5f - logoHeight) * 0.5f;
2020-04-05 21:16:16 +02:00
auto const y2 = y1 + logoHeight;
2020-04-06 07:36:03 +02:00
// calculate uv coords
2020-04-05 21:16:16 +02:00
auto const uv1 = ImVec2 (subTex->left, subTex->top);
auto const uv2 = ImVec2 (subTex->right, subTex->bottom);
2020-04-06 07:36:03 +02:00
// draw to top screen
2020-04-05 21:16:16 +02:00
ImGui::GetBackgroundDrawList ()->AddImage (
&s_gfxTexture, ImVec2 (x1, y1), ImVec2 (x2, y2), uv1, uv2);
2020-04-06 07:36:03 +02:00
// draw to bottom screen
2020-04-05 21:16:16 +02:00
ImGui::GetBackgroundDrawList ()->AddImage (&s_gfxTexture,
ImVec2 (x1, y1 + screenHeight * 0.5f),
ImVec2 (x2, y2 + screenHeight * 0.5f),
2020-04-05 21:16:16 +02:00
uv1,
uv2);
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
}
2020-04-06 07:36:03 +02:00
/// \brief Draw status
2020-04-05 21:16:16 +02:00
void drawStatus ()
{
2020-04-17 22:32:39 +02:00
#ifndef CLASSIC
2020-04-05 21:16:16 +02:00
constexpr unsigned batteryLevels[] = {
gfx_battery0_idx,
gfx_battery0_idx,
gfx_battery1_idx,
gfx_battery2_idx,
gfx_battery3_idx,
gfx_battery4_idx,
};
constexpr unsigned wifiLevels[] = {
gfx_wifiNull_idx,
gfx_wifi1_idx,
gfx_wifi2_idx,
gfx_wifi3_idx,
};
2020-04-06 07:36:03 +02:00
// get battery charging state or level
2020-04-05 21:16:16 +02:00
static u8 charging = 0;
static u8 level = 5;
PTMU_GetBatteryChargeState (&charging);
if (!charging)
{
PTMU_GetBatteryLevel (&level);
if (level >= std::extent_v<decltype (batteryLevels)>)
svcBreak (USERBREAK_PANIC);
}
auto const &io = ImGui::GetIO ();
auto const &style = ImGui::GetStyle ();
auto const screenWidth = io.DisplaySize.x;
2020-04-06 07:36:03 +02:00
// calculate battery icon metrics
2020-04-05 21:16:16 +02:00
auto const battery =
Tex3DS_GetSubTexture (s_gfxT3x, charging ? gfx_batteryCharge_idx : batteryLevels[level]);
2020-04-07 03:24:06 +02:00
auto const batteryWidth = battery->width;
auto const batteryHeight = battery->height;
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// calculate battery icon position
2020-04-05 21:16:16 +02:00
auto const p1 = ImVec2 (screenWidth - batteryWidth, 0.0f);
2020-04-23 00:15:23 +02:00
auto const p2 = ImVec2 (p1.x + batteryWidth, p1.y + batteryHeight);
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// calculate battery icon uv coords
2020-04-05 21:16:16 +02:00
auto const uv1 = ImVec2 (battery->left, battery->top);
auto const uv2 = ImVec2 (battery->right, battery->bottom);
2020-04-06 07:36:03 +02:00
// draw battery icon
2020-04-10 07:46:46 +02:00
ImGui::GetForegroundDrawList ()->AddImage (
&s_gfxTexture, p1, p2, uv1, uv2, ImGui::GetColorU32 (ImGuiCol_Text));
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// get wifi strength
2020-04-05 21:16:16 +02:00
auto const wifiStrength = osGetWifiStrength ();
2020-04-06 07:36:03 +02:00
// calculate wifi icon metrics
2020-04-05 21:16:16 +02:00
auto const wifi = Tex3DS_GetSubTexture (s_gfxT3x, wifiLevels[wifiStrength]);
2020-04-07 03:24:06 +02:00
auto const wifiWidth = wifi->width;
auto const wifiHeight = wifi->height;
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// calculate wifi icon position
2020-04-23 00:15:23 +02:00
auto const p3 = ImVec2 (p1.x - wifiWidth - style.FramePadding.x, 0.0f);
auto const p4 = ImVec2 (p3.x + wifiWidth, p3.y + wifiHeight);
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// calculate wifi icon uv coords
2020-04-05 21:16:16 +02:00
auto const uv3 = ImVec2 (wifi->left, wifi->top);
auto const uv4 = ImVec2 (wifi->right, wifi->bottom);
2020-04-06 07:36:03 +02:00
// draw wifi icon
2020-04-10 07:46:46 +02:00
ImGui::GetForegroundDrawList ()->AddImage (
&s_gfxTexture, p3, p4, uv3, uv4, ImGui::GetColorU32 (ImGuiCol_Text));
2020-04-05 21:16:16 +02:00
2020-04-06 07:36:03 +02:00
// draw current timestamp
2020-04-17 22:32:39 +02:00
char timeBuffer[16];
2020-04-05 21:16:16 +02:00
auto const now = std::time (nullptr);
2020-04-17 22:32:39 +02:00
std::strftime (timeBuffer, sizeof (timeBuffer), "%H:%M:%S", std::localtime (&now));
2020-04-23 00:15:23 +02:00
// draw free space
char buffer[64];
auto const freeSpace = FtpServer::getFreeSpace ();
2020-04-17 22:32:39 +02:00
2020-04-23 00:15:23 +02:00
std::snprintf (
buffer, sizeof (buffer), "%s %s", timeBuffer, freeSpace.empty () ? "" : freeSpace.c_str ());
2020-04-17 22:32:39 +02:00
2020-04-23 00:15:23 +02:00
auto const size = ImGui::CalcTextSize (buffer);
auto const p5 = ImVec2 (p3.x - size.x - style.FramePadding.x, style.FramePadding.y);
ImGui::GetForegroundDrawList ()->AddText (p5, ImGui::GetColorU32 (ImGuiCol_Text), buffer);
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
}
}
bool platform::init ()
{
2020-04-06 07:36:03 +02:00
// enable New 3DS speedup
2020-04-05 21:16:16 +02:00
osSetSpeedupEnable (true);
acInit ();
ptmuInit ();
2020-04-17 22:32:39 +02:00
#ifndef CLASSIC
2020-04-05 21:16:16 +02:00
romfsInit ();
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
gfxInitDefault ();
gfxSet3D (false);
2020-04-17 22:32:39 +02:00
#ifdef CLASSIC
consoleInit (GFX_TOP, &g_statusConsole);
consoleInit (GFX_TOP, &g_logConsole);
consoleInit (GFX_BOTTOM, &g_sessionConsole);
consoleSetWindow (&g_statusConsole, 0, 0, 50, 1);
consoleSetWindow (&g_logConsole, 0, 1, 50, 29);
consoleSetWindow (&g_sessionConsole, 0, 0, 40, 30);
#endif
2020-04-05 21:16:16 +02:00
#ifndef NDEBUG
consoleDebugInit (debugDevice_SVC);
std::setvbuf (stderr, nullptr, _IOLBF, 0);
#endif
2020-04-28 02:57:09 +02:00
aptHook (&s_aptHookCookie, handleAPTHook, nullptr);
2020-04-17 22:32:39 +02:00
#ifndef CLASSIC
// initialize citro3d
2020-04-24 05:50:36 +02:00
C3D_Init (2 * C3D_DEFAULT_CMDBUF_SIZE);
// create top screen render target
s_top =
C3D_RenderTargetCreate (FB_HEIGHT * 0.5f, FB_WIDTH, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
C3D_RenderTargetSetOutput (s_top, GFX_TOP, GFX_LEFT, DISPLAY_TRANSFER_FLAGS);
// create bottom screen render target
s_bottom = C3D_RenderTargetCreate (
FB_HEIGHT * 0.5f, FB_WIDTH * 0.8f, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
C3D_RenderTargetSetOutput (s_bottom, GFX_BOTTOM, GFX_LEFT, DISPLAY_TRANSFER_FLAGS);
2020-04-05 21:16:16 +02:00
if (!imgui::ctru::init ())
return false;
imgui::citro3d::init ();
{
2020-04-06 07:36:03 +02:00
// load texture atlas
2020-04-05 21:16:16 +02:00
fs::File file;
if (!file.open ("romfs:/gfx.t3x"))
svcBreak (USERBREAK_PANIC);
s_gfxT3x = Tex3DS_TextureImportStdio (file, &s_gfxTexture, nullptr, true);
if (!s_gfxT3x)
svcBreak (USERBREAK_PANIC);
2020-04-06 07:36:03 +02:00
C3D_TexSetFilter (&s_gfxTexture, GPU_LINEAR, GPU_LINEAR);
}
2020-04-05 21:16:16 +02:00
auto &io = ImGui::GetIO ();
auto &style = ImGui::GetStyle ();
// disable imgui.ini file
io.IniFilename = nullptr;
2020-04-07 03:24:06 +02:00
// citro3d logo doesn't quite show with the default transparency
style.Colors[ImGuiCol_WindowBg].w = 0.8f;
style.ScaleAllSizes (0.5f);
2020-04-17 22:32:39 +02:00
#endif
2020-04-07 03:24:06 +02:00
2020-04-05 21:16:16 +02:00
return true;
}
bool platform::networkVisible ()
{
// check if soc:u is active
if (!s_socuActive)
return false;
return getNetworkVisibility ();
}
2020-04-05 21:16:16 +02:00
bool platform::loop ()
{
if (!aptMainLoop ())
return false;
startNetwork ();
hidScanInput ();
2020-04-28 02:57:09 +02:00
auto const kDown = hidKeysDown ();
// check if the user wants to exit
if (kDown & KEY_START)
2020-04-05 21:16:16 +02:00
return false;
2020-04-28 02:57:09 +02:00
// check if the user wants to toggle the backlight
if (kDown & KEY_SELECT)
{
s_backlight = !s_backlight;
enableBacklight (s_backlight);
}
2020-04-17 22:32:39 +02:00
#ifndef CLASSIC
auto &io = ImGui::GetIO ();
// setup display metrics
io.DisplaySize = ImVec2 (SCREEN_WIDTH, SCREEN_HEIGHT);
io.DisplayFramebufferScale = ImVec2 (FB_SCALE, FB_SCALE);
2020-04-05 21:16:16 +02:00
imgui::ctru::newFrame ();
ImGui::NewFrame ();
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
return true;
}
void platform::render ()
{
drawLogo ();
drawStatus ();
2020-04-17 22:32:39 +02:00
#ifdef CLASSIC
gfxFlushBuffers ();
gspWaitForVBlank ();
gfxSwapBuffers ();
#else
2020-04-05 21:16:16 +02:00
ImGui::Render ();
C3D_FrameBegin (C3D_FRAME_SYNCDRAW);
// clear frame/depth buffers
C3D_RenderTargetClear (s_top, C3D_CLEAR_ALL, CLEAR_COLOR, 0);
C3D_RenderTargetClear (s_bottom, C3D_CLEAR_ALL, CLEAR_COLOR, 0);
imgui::citro3d::render (s_top, s_bottom);
C3D_FrameEnd (0);
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
}
void platform::exit ()
{
2020-04-17 22:32:39 +02:00
#ifndef CLASSIC
imgui::citro3d::exit ();
// free graphics
2020-04-05 21:16:16 +02:00
Tex3DS_TextureFree (s_gfxT3x);
C3D_TexDelete (&s_gfxTexture);
// free render targets
C3D_RenderTargetDelete (s_bottom);
C3D_RenderTargetDelete (s_top);
// deinitialize citro3d
C3D_Fini ();
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
2020-04-07 04:17:30 +02:00
if (s_socuActive)
socExit ();
2020-04-05 21:16:16 +02:00
s_socuActive = false;
std::free (s_socuBuffer);
2020-04-28 02:57:09 +02:00
aptUnhook (&s_aptHookCookie);
// turn backlight back on
if (!s_backlight)
enableBacklight (true);
2020-04-05 21:16:16 +02:00
gfxExit ();
2020-04-17 22:32:39 +02:00
#ifndef CLASSIC
2020-04-17 01:36:41 +02:00
romfsExit ();
2020-04-17 22:32:39 +02:00
#endif
2020-04-05 21:16:16 +02:00
ptmuExit ();
acExit ();
}
2020-04-17 22:32:39 +02:00
///////////////////////////////////////////////////////////////////////////
platform::steady_clock::time_point platform::steady_clock::now () noexcept
{
return time_point (duration (svcGetSystemTick ()));
}
2020-04-05 21:16:16 +02:00
///////////////////////////////////////////////////////////////////////////
2020-04-06 07:36:03 +02:00
/// \brief Platform thread pimpl
2020-04-05 21:16:16 +02:00
class platform::Thread::privateData_t
{
public:
privateData_t ()
{
if (thread)
threadFree (thread);
}
2020-04-06 07:36:03 +02:00
/// \brief Parameterized constructor
/// \param func_ Thread entry point
2020-04-07 04:17:30 +02:00
privateData_t (std::function<void ()> &&func_) : thread (nullptr), func (std::move (func_))
2020-04-05 21:16:16 +02:00
{
// use next-lower priority
2020-04-05 21:16:16 +02:00
s32 priority = 0x30;
svcGetThreadPriority (&priority, CUR_THREAD_HANDLE);
priority = std::clamp<s32> (priority, 0x18, 0x3F - 1) + 1;
2020-04-05 21:16:16 +02:00
// use appcore
thread = threadCreate (&privateData_t::threadFunc, this, STACK_SIZE, priority, 0, false);
2020-04-05 21:16:16 +02:00
assert (thread);
}
2020-04-06 07:36:03 +02:00
/// \brief Underlying thread entry point
/// \param arg_ Thread pimpl object
2020-04-05 21:16:16 +02:00
static void threadFunc (void *const arg_)
{
2020-04-06 07:36:03 +02:00
// call passed-in entry point
2020-04-05 21:16:16 +02:00
auto const t = static_cast<privateData_t *> (arg_);
t->func ();
}
2020-04-06 07:36:03 +02:00
/// \brief Underlying thread
2020-04-05 21:16:16 +02:00
::Thread thread = nullptr;
2020-04-06 07:36:03 +02:00
/// \brief Thread entry point
2020-04-05 21:16:16 +02:00
std::function<void ()> func;
};
///////////////////////////////////////////////////////////////////////////
platform::Thread::~Thread () = default;
platform::Thread::Thread () : m_d (new privateData_t ())
{
}
2020-04-07 04:17:30 +02:00
platform::Thread::Thread (std::function<void ()> &&func_)
: m_d (new privateData_t (std::move (func_)))
2020-04-05 21:16:16 +02:00
{
}
platform::Thread::Thread (Thread &&that_) : m_d (new privateData_t ())
{
std::swap (m_d, that_.m_d);
}
platform::Thread &platform::Thread::operator= (Thread &&that_)
{
std::swap (m_d, that_.m_d);
return *this;
}
void platform::Thread::join ()
{
threadJoin (m_d->thread, UINT64_MAX);
}
void platform::Thread::sleep (std::chrono::milliseconds const timeout_)
{
svcSleepThread (std::chrono::nanoseconds (timeout_).count ());
}
///////////////////////////////////////////////////////////////////////////
2020-04-06 07:36:03 +02:00
/// \brief Platform mutex pimpl
2020-04-05 21:16:16 +02:00
class platform::Mutex::privateData_t
{
public:
2020-04-06 07:36:03 +02:00
/// \brief Underlying mutex
2020-04-05 21:16:16 +02:00
LightLock mutex;
};
///////////////////////////////////////////////////////////////////////////
platform::Mutex::~Mutex () = default;
platform::Mutex::Mutex () : m_d (new privateData_t ())
{
LightLock_Init (&m_d->mutex);
}
void platform::Mutex::lock ()
{
LightLock_Lock (&m_d->mutex);
}
void platform::Mutex::unlock ()
{
LightLock_Unlock (&m_d->mutex);
}