CArchSleepUnix.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2002 Chris Schoeneman
00004  * 
00005  * This package is free software you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
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 // CArchSleepUnix
00041 //
00042 
00043 CArchSleepUnix::CArchSleepUnix()
00044 {
00045     // do nothing
00046 }
00047 
00048 CArchSleepUnix::~CArchSleepUnix()
00049 {
00050     // do nothing
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     // prep timeout
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     // wait
00068     while (nanosleep(&t, &t) < 0)
00069         ARCH->testCancelThread();
00070 #else
00071     /* emulate nanosleep() with select() */
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 }

Generated on Fri Nov 6 00:18:44 2009 for synergy-plus by  doxygen 1.4.7