Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ if(NINTENDO_SWITCH)
target_sources(${FTPD_TARGET} PRIVATE
source/switch/init.c
source/switch/platform.cpp
source/common/thread.cpp
)

if(FTPD_CLASSIC)
Expand Down
99 changes: 99 additions & 0 deletions source/common/thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "platform.h"

#include <mutex>
#include <thread>


///////////////////////////////////////////////////////////////////////////
/// \brief Platform thread pimpl
class platform::Thread::privateData_t
{
public:
privateData_t () = default;

/// \brief Parameterized constructor
/// \param func_ Thread entry point
privateData_t (std::function<void ()> &&func_) : thread (std::move (func_))
{
}

/// \brief Underlying thread
std::thread thread;
};

///////////////////////////////////////////////////////////////////////////
platform::Thread::~Thread () = default;

platform::Thread::Thread () : m_d (new privateData_t ())
{
}

platform::Thread::Thread (std::function<void ()> &&func_)
: m_d (new privateData_t (std::move (func_)))
{
}

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 ()
{
m_d->thread.join ();
}

void platform::Thread::sleep (std::chrono::milliseconds const timeout_)
{
std::this_thread::sleep_for (timeout_);
}

///////////////////////////////////////////////////////////////////////////
#define USE_STD_MUTEX 1

/// \brief Platform mutex pimpl
class platform::Mutex::privateData_t
{
public:
#if USE_STD_MUTEX
/// \brief Underlying mutex
std::mutex mutex;
#else
/// \brief Underlying mutex
::Mutex mutex;
#endif
};

///////////////////////////////////////////////////////////////////////////
platform::Mutex::~Mutex () = default;

platform::Mutex::Mutex () : m_d (new privateData_t ())
{
#if !USE_STD_MUTEX
mutexInit (&m_d->mutex);
#endif
}

void platform::Mutex::lock ()
{
#if USE_STD_MUTEX
m_d->mutex.lock ();
#else
mutexLock (&m_d->mutex);
#endif
}

void platform::Mutex::unlock ()
{
#if USE_STD_MUTEX
m_d->mutex.unlock ();
#else
mutexUnlock (&m_d->mutex);
#endif
}
13 changes: 8 additions & 5 deletions source/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,19 @@ void addLog (LogLevel const level_, char const *const fmt_, va_list ap_)
return;
#endif

#ifndef __NDS__
thread_local
#if HAVE_MUTEX
auto const lock = std::scoped_lock (s_lock);
#endif
static char buffer[1024];

std::vsnprintf (buffer, sizeof (buffer), fmt_, ap_);

#ifndef __NDS__
auto const lock = std::scoped_lock (s_lock);
#define BUFFER_SIZE 1024
#else
#define BUFFER_SIZE 256
#endif
static char buffer[BUFFER_SIZE];
std::vsnprintf (buffer, sizeof (buffer), fmt_, ap_);

#ifndef NDEBUG
// std::fprintf (stderr, "%s", s_prefix[level_]);
// std::fputs (buffer, stderr);
Expand Down
93 changes: 0 additions & 93 deletions source/switch/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,96 +922,3 @@ void platform::exit ()
}
}

///////////////////////////////////////////////////////////////////////////
/// \brief Platform thread pimpl
class platform::Thread::privateData_t
{
public:
privateData_t () = default;

/// \brief Parameterized constructor
/// \param func_ Thread entry point
privateData_t (std::function<void ()> &&func_) : thread (std::move (func_))
{
}

/// \brief Underlying thread
std::thread thread;
};

///////////////////////////////////////////////////////////////////////////
platform::Thread::~Thread () = default;

platform::Thread::Thread () : m_d (new privateData_t ())
{
}

platform::Thread::Thread (std::function<void ()> &&func_)
: m_d (new privateData_t (std::move (func_)))
{
}

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 ()
{
m_d->thread.join ();
}

void platform::Thread::sleep (std::chrono::milliseconds const timeout_)
{
std::this_thread::sleep_for (timeout_);
}

///////////////////////////////////////////////////////////////////////////
#define USE_STD_MUTEX 1

/// \brief Platform mutex pimpl
class platform::Mutex::privateData_t
{
public:
#if USE_STD_MUTEX
/// \brief Underlying mutex
std::mutex mutex;
#else
/// \brief Underlying mutex
::Mutex mutex;
#endif
};

///////////////////////////////////////////////////////////////////////////
platform::Mutex::~Mutex () = default;

platform::Mutex::Mutex () : m_d (new privateData_t ())
{
#if !USE_STD_MUTEX
mutexInit (&m_d->mutex);
#endif
}

void platform::Mutex::lock ()
{
#if USE_STD_MUTEX
m_d->mutex.lock ();
#else
mutexLock (&m_d->mutex);
#endif
}

void platform::Mutex::unlock ()
{
#if USE_STD_MUTEX
m_d->mutex.unlock ();
#else
mutexUnlock (&m_d->mutex);
#endif
}