tahoma2d/toonz/sources/stdfx/igs_resource_thread_win.cpp

66 lines
2.4 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include <stdexcept> /* std::domain_error */
#include "igs_resource_thread.h"
#include "igs_resource_msg_from_err.h"
2016-06-15 18:43:10 +12:00
const HANDLE igs::resource::thread_run(unsigned(__stdcall *function)(void *),
void *func_arg, const int priority) {
// unsigned thread_addr=0;
HANDLE thread_id = reinterpret_cast<HANDLE>(
::_beginthreadex(NULL, 0, function, func_arg, 0, NULL));
if (0 == thread_id) {
throw std::domain_error(
igs_resource_msg_from_err(TEXT("_beginthreadex(-)"), errno));
}
/*
vc2005 MSDN
BOOL SetThreadPriority(int nPriority);
THREAD_PRIORITY_TIME_CRITICAL 15 or 31
THREAD_PRIORITY_HIGHEST 2
THREAD_PRIORITY_ABOVE_NORMAL 1
THREAD_PRIORITY_NORMAL -1
THREAD_PRIORITY_BELOW_NORMAL 1
THREAD_PRIORITY_LOWEST 2
THREAD_PRIORITY_IDLE 1 or 16
*/
if (0 == ::SetThreadPriority(thread_id, priority)) {
throw std::domain_error(igs_resource_msg_from_err(
TEXT("SetThreadPriority(-)"), ::GetLastError()));
}
return thread_id;
2016-03-19 06:57:51 +13:00
}
/*
threadの終了を見るよりも
thread内で終了時に終了スイッチを入れる方法で感知する
linuxでのやり方にあわせる2011-03-30
*/
2016-06-15 18:43:10 +12:00
const bool igs::resource::thread_was_done(const HANDLE thread_id) {
DWORD exit_code = 0;
if (0 == ::GetExitCodeThread(thread_id, &exit_code)) {
throw std::domain_error(
igs_resource_msg_from_err("GetExitCodeThread(-)", ::GetLastError()));
}
if (exit_code == STILL_ACTIVE) {
return false;
}
return true;
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
void igs::resource::thread_join(const HANDLE thread_id) {
thread_wait(thread_id);
thread_close(thread_id);
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
void igs::resource::thread_wait(const HANDLE thread_id) {
/* _endthreadex(-)はスレッドハンドルを閉じない??? */
if (WAIT_FAILED == ::WaitForSingleObject(thread_id, INFINITE)) {
throw std::domain_error(igs_resource_msg_from_err(
TEXT("WaitForSingleObject(-)"), ::GetLastError()));
}
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
void igs::resource::thread_close(const HANDLE thread_id) {
if (0 == ::CloseHandle(thread_id)) {
throw std::domain_error(
igs_resource_msg_from_err(TEXT("CloseHandle(-)"), ::GetLastError()));
}
2016-03-19 06:57:51 +13:00
}