Toshihiro Shimizu 890ddd
#include <stdexcept> /* std::domain_error */</stdexcept>
Toshihiro Shimizu 890ddd
#include "igs_resource_thread.h"
Toshihiro Shimizu 890ddd
#include "igs_resource_msg_from_err.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const HANDLE igs::resource::thread_run(unsigned(__stdcall *function)(void *),
Shinya Kitaoka 120a6e
                                       void *func_arg, const int priority) {
Shinya Kitaoka 120a6e
  // unsigned thread_addr=0;
Shinya Kitaoka 120a6e
  HANDLE thread_id = reinterpret_cast<handle>(</handle>
Shinya Kitaoka 120a6e
      ::_beginthreadex(NULL, 0, function, func_arg, 0, NULL));
Shinya Kitaoka 120a6e
  if (0 == thread_id) {
Shinya Kitaoka 120a6e
    throw std::domain_error(
Shinya Kitaoka 120a6e
        igs_resource_msg_from_err(TEXT("_beginthreadex(-)"), errno));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
  vc2005 MSDN より
Shinya Kitaoka 120a6e
  BOOL SetThreadPriority(int nPriority);
Shinya Kitaoka 120a6e
  に与える値を優先度の高いものから順に並べます。
Shinya Kitaoka 120a6e
  THREAD_PRIORITY_TIME_CRITICAL		プロセスにより15 or 31
Shinya Kitaoka 120a6e
  THREAD_PRIORITY_HIGHEST			標準より2ポイント高い
Shinya Kitaoka 120a6e
  THREAD_PRIORITY_ABOVE_NORMAL		標準より1ポイント高い
Shinya Kitaoka 120a6e
  THREAD_PRIORITY_NORMAL		-1	標準
Shinya Kitaoka 120a6e
  THREAD_PRIORITY_BELOW_NORMAL		標準より1ポイント低い
Shinya Kitaoka 120a6e
  THREAD_PRIORITY_LOWEST			標準より2ポイント低い
Shinya Kitaoka 120a6e
  THREAD_PRIORITY_IDLE			プロセスにより1 or 16
Shinya Kitaoka 120a6e
  */
Shinya Kitaoka 120a6e
  if (0 == ::SetThreadPriority(thread_id, priority)) {
Shinya Kitaoka 120a6e
    throw std::domain_error(igs_resource_msg_from_err(
Shinya Kitaoka 120a6e
        TEXT("SetThreadPriority(-)"), ::GetLastError()));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return thread_id;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
以下の関数でthreadの終了を見るよりも、
Toshihiro Shimizu 890ddd
thread内で終了時に終了スイッチを入れる方法で感知する。
Toshihiro Shimizu 890ddd
linuxでのやり方にあわせる。2011-03-30
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
const bool igs::resource::thread_was_done(const HANDLE thread_id) {
Shinya Kitaoka 120a6e
  DWORD exit_code = 0;
Shinya Kitaoka 120a6e
  if (0 == ::GetExitCodeThread(thread_id, &exit_code)) {
Shinya Kitaoka 120a6e
    throw std::domain_error(
Shinya Kitaoka 120a6e
        igs_resource_msg_from_err("GetExitCodeThread(-)", ::GetLastError()));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (exit_code == STILL_ACTIVE) {
Shinya Kitaoka 120a6e
    return false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
void igs::resource::thread_join(const HANDLE thread_id) {
Shinya Kitaoka 120a6e
  thread_wait(thread_id);
Shinya Kitaoka 120a6e
  thread_close(thread_id);
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
void igs::resource::thread_wait(const HANDLE thread_id) {
Shinya Kitaoka 120a6e
  /* _endthreadex(-)はスレッドハンドルを閉じない??? */
Shinya Kitaoka 120a6e
  if (WAIT_FAILED == ::WaitForSingleObject(thread_id, INFINITE)) {
Shinya Kitaoka 120a6e
    throw std::domain_error(igs_resource_msg_from_err(
Shinya Kitaoka 120a6e
        TEXT("WaitForSingleObject(-)"), ::GetLastError()));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Shinya Kitaoka 120a6e
void igs::resource::thread_close(const HANDLE thread_id) {
Shinya Kitaoka 120a6e
  if (0 == ::CloseHandle(thread_id)) {
Shinya Kitaoka 120a6e
    throw std::domain_error(
Shinya Kitaoka 120a6e
        igs_resource_msg_from_err(TEXT("CloseHandle(-)"), ::GetLastError()));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}