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