CPUThread fixes, thread_t cleanup

This commit is contained in:
Nekotekina 2015-07-06 22:35:34 +03:00
parent 83321c5be7
commit eafddd9e33
10 changed files with 69 additions and 83 deletions

View file

@ -19,7 +19,7 @@ std::unique_ptr<wchar_t[]> to_wchar(const std::string& source)
if (!MultiByteToWideChar(CP_UTF8, 0, source.c_str(), size, buffer.get(), size))
{
throw EXCEPTION("Conversion failed (0x%llx)", GET_API_ERROR);
throw EXCEPTION("System error 0x%x", GetLastError());
}
return buffer;
@ -38,14 +38,14 @@ void to_utf8(std::string& result, const wchar_t* source)
if (size <= 0)
{
throw EXCEPTION("Conversion failed (0x%llx)", GET_API_ERROR);
throw EXCEPTION("System error 0x%x", GetLastError());
}
result.resize(size);
if (!WideCharToMultiByte(CP_UTF8, 0, source, length, &result.front(), size, NULL, NULL))
{
throw EXCEPTION("Conversion failed (0x%llx)", GET_API_ERROR);
throw EXCEPTION("System error 0x%x", GetLastError());
}
}

View file

@ -1307,14 +1307,11 @@ void thread_t::start(std::function<std::string()> name, std::function<void()> fu
}
//ctrl->set_current(false);
vm::reservation_free();
g_thread_count--;
ctrl->joinable = false;
ctrl->join_cv.notify_all();
#if defined(_MSC_VER)
_set_se_translator(old_se_translator);
#endif
@ -1331,39 +1328,16 @@ void thread_t::detach()
// +clear m_thread
const auto ctrl = std::move(m_thread);
cv.notify_all();
{
// lock for reliable notification
std::lock_guard<std::mutex> lock(mutex);
cv.notify_all();
}
ctrl->m_thread.detach();
}
void thread_t::join(std::unique_lock<std::mutex>& lock)
{
if (!m_thread)
{
throw EXCEPTION("Invalid thread");
}
if (g_tls_this_thread == m_thread.get())
{
throw EXCEPTION("Deadlock");
}
// +clear m_thread
const auto ctrl = std::move(m_thread);
cv.notify_all();
// wait for completion
while (ctrl->joinable)
{
CHECK_EMU_STATUS;
ctrl->join_cv.wait_for(lock, std::chrono::milliseconds(1));
}
ctrl->m_thread.join();
}
void thread_t::join()
{
if (!m_thread)
@ -1379,7 +1353,12 @@ void thread_t::join()
// +clear m_thread
const auto ctrl = std::move(m_thread);
cv.notify_all();
{
// lock for reliable notification
std::lock_guard<std::mutex> lock(mutex);
cv.notify_all();
}
ctrl->m_thread.join();
}

View file

@ -13,12 +13,6 @@ class thread_ctrl_t final
// name getter
const std::function<std::string()> name;
// condition variable, notified before thread exit
std::condition_variable join_cv;
// thread status (set to false after execution)
std::atomic<bool> joinable{ true };
// true if TLS of some thread points to owner
std::atomic<bool> assigned{ false };
@ -71,9 +65,6 @@ public:
// detach thread -> empty state
void detach();
// join thread (provide locked unique_lock, for example, lv2_lock, for interruptibility) -> empty state
void join(std::unique_lock<std::mutex>& lock);
// join thread -> empty state
void join();