This commit is contained in:
Nekotekina 2015-08-21 14:07:31 +03:00
parent 5e14310071
commit e551e2bc5d
12 changed files with 107 additions and 37 deletions

View file

@ -28,8 +28,6 @@
#if defined(_MSC_VER) && _MSC_VER <= 1800
#define alignas(x) _CRT_ALIGN(x)
#elif defined(__GNUG__)
#define ALIGN(x) __attribute__((aligned(x))) // not used
#endif
#if defined(__GNUG__)
@ -83,7 +81,7 @@ int clock_gettime(clockid_t clk_id, struct timespec *tp);
#endif /* __GNUG__ */
#if defined(_MSC_VER)
// Unsigned 128-bit number implementation
// Unsigned 128-bit integer implementation
struct alignas(16) uint128_t
{
uint64_t lo, hi;

View file

@ -248,7 +248,7 @@ namespace fmt
// pointer to the current buffer
char* buf_addr = fixed_buf.data();
for (std::size_t buf_size = fixed_buf.size();; buf_size *= 2, buf.reset(buf_addr = new char[buf_size]))
for (std::size_t buf_size = fixed_buf.size();; buf.reset(buf_addr = new char[buf_size]))
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
@ -256,10 +256,17 @@ namespace fmt
#pragma GCC diagnostic pop
if (len > INT_MAX)
{
throw std::runtime_error("std::snprintf() failed");
}
if (len <= buf_size)
{
return{ buf_addr, len };
}
buf_size = len;
}
}
@ -285,11 +292,6 @@ namespace fmt
std::memcpy(message.get(), other, size + 1);
}
exception(exception&& other)
{
message = std::move(other.message);
}
operator const char*() const
{
return message.get();

View file

@ -1196,7 +1196,7 @@ const thread_ctrl_t* get_current_thread_ctrl()
std::string thread_ctrl_t::get_name() const
{
return name();
return m_name();
}
named_thread_t::named_thread_t(std::function<std::string()> name, std::function<void()> func)
@ -1220,12 +1220,12 @@ std::string named_thread_t::get_name() const
throw EXCEPTION("Invalid thread");
}
if (!m_thread->name)
if (!m_thread->m_name)
{
throw EXCEPTION("Invalid name getter");
}
return m_thread->name();
return m_thread->m_name();
}
std::atomic<u32> g_thread_count{ 0 };
@ -1296,6 +1296,13 @@ void named_thread_t::start(std::function<std::string()> name, std::function<void
LOG_NOTICE(GENERAL, "Thread aborted");
}
for (auto& func : ctrl->m_atexit)
{
func();
func = nullptr;
}
vm::reservation_free();
g_thread_count--;

View file

@ -7,15 +7,20 @@ class thread_ctrl_t final
{
friend class named_thread_t;
template<typename T> friend void current_thread_register_atexit(T);
// thread handler
std::thread m_thread;
// name getter
const std::function<std::string()> name;
const std::function<std::string()> m_name;
// functions executed at thread exit (temporarily)
std::vector<std::function<void()>> m_atexit;
public:
thread_ctrl_t(std::function<std::string()> name)
: name(std::move(name))
: m_name(std::move(name))
{
}
@ -23,6 +28,14 @@ public:
std::string get_name() const;
};
// register function at thread exit (temporarily)
template<typename T> void current_thread_register_atexit(T func)
{
extern thread_local thread_ctrl_t* g_tls_this_thread;
g_tls_this_thread->m_atexit.emplace_back(func);
}
class named_thread_t
{
// pointer to managed resource (shared with actual thread)