Shinya Kitaoka 120a6e
#include <stdexcept>  // std::domain_error()</stdexcept>
Shinya Kitaoka 120a6e
#include <cerrno>     // errno</cerrno>
Toshihiro Shimizu 890ddd
#include "igs_resource_msg_from_err.h"
Toshihiro Shimizu 890ddd
#include "igs_resource_sleep.h"
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
1second=1,000milli_seconds=1,000,000micro_seconds=1,000,000,000nano_seconds
Toshihiro Shimizu 890ddd
tv_sec(seconds)が負か、tv_nsec(nano_seconds)が0〜999,999,999範囲外、はエラー
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void igs::resource::sleep_sn(const time_t seconds, const long nano_seconds) {
Shinya Kitaoka 120a6e
  struct timespec req;
Shinya Kitaoka 120a6e
  req.tv_sec  = seconds;
Shinya Kitaoka 120a6e
  req.tv_nsec = nano_seconds;
Shinya Kitaoka 120a6e
  struct timespec rem;
Shinya Kitaoka 120a6e
  rem.tv_sec  = 0;
Shinya Kitaoka 120a6e
  rem.tv_nsec = 0;
Shinya Kitaoka 120a6e
  if (::nanosleep(&req, &rem) < 0) {
Shinya Kitaoka 120a6e
    throw std::domain_error(
Shinya Kitaoka 120a6e
        igs_resource_msg_from_err(TEXT("nanosleep(-)"), errno));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
windows ::Sleep()の仕様にあわせるためだけの関数
Toshihiro Shimizu 890ddd
1second = 1,000milli_seconds(ミリ秒)
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void igs::resource::sleep_m(const DWORD milli_seconds) {
Shinya Kitaoka 120a6e
  const time_t seconds    = milli_seconds / 1000;
Shinya Kitaoka 120a6e
  const long nano_seconds = (milli_seconds % 1000) * 1000000;
Shinya Kitaoka 120a6e
  // std::cout << "milli_seconds=" << milli_seconds << std::endl;
Shinya Kitaoka 120a6e
  // std::cout << "      seconds=" << seconds << std::endl;
Shinya Kitaoka 120a6e
  // std::cout << " nano_seconds=" << nano_seconds << std::endl;
Shinya Kitaoka 120a6e
  igs::resource::sleep_sn(seconds, nano_seconds);
Toshihiro Shimizu 890ddd
}