00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "CArchSleepUnix.h"
00020 #include "CArch.h"
00021 #if TIME_WITH_SYS_TIME
00022 # include <sys/time.h>
00023 # include <time.h>
00024 #else
00025 # if HAVE_SYS_TIME_H
00026 # include <sys/time.h>
00027 # else
00028 # include <time.h>
00029 # endif
00030 #endif
00031 #if !HAVE_NANOSLEEP
00032 # if HAVE_SYS_SELECT_H
00033 # include <sys/select.h>
00034 # endif
00035 # if HAVE_SYS_TYPES_H
00036 # include <sys/types.h>
00037 # endif
00038 # if HAVE_UNISTD_H
00039 # include <unistd.h>
00040 # endif
00041 #endif
00042
00043
00044
00045
00046
00047 CArchSleepUnix::CArchSleepUnix()
00048 {
00049
00050 }
00051
00052 CArchSleepUnix::~CArchSleepUnix()
00053 {
00054
00055 }
00056
00057 void
00058 CArchSleepUnix::sleep(double timeout)
00059 {
00060 ARCH->testCancelThread();
00061 if (timeout < 0.0) {
00062 return;
00063 }
00064
00065 #if HAVE_NANOSLEEP
00066
00067 struct timespec t;
00068 t.tv_sec = (long)timeout;
00069 t.tv_nsec = (long)(1.0e+9 * (timeout - (double)t.tv_sec));
00070
00071
00072 while (nanosleep(&t, &t) < 0)
00073 ARCH->testCancelThread();
00074 #else
00075
00076 double startTime = ARCH->time();
00077 double timeLeft = timeout;
00078 while (timeLeft > 0.0) {
00079 struct timeval timeout2;
00080 timeout2.tv_sec = static_cast<int>(timeLeft);
00081 timeout2.tv_usec = static_cast<int>(1.0e+6 * (timeLeft -
00082 timeout2.tv_sec));
00083 select((SELECT_TYPE_ARG1) 0,
00084 SELECT_TYPE_ARG234 NULL,
00085 SELECT_TYPE_ARG234 NULL,
00086 SELECT_TYPE_ARG234 NULL,
00087 SELECT_TYPE_ARG5 &timeout2);
00088 ARCH->testCancelThread();
00089 timeLeft = timeout - (ARCH->time() - startTime);
00090 }
00091 #endif
00092 }