• Main Page
  • Classes
  • Files
  • File List

CArchSleepUnix.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2002 Chris Schoeneman
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 // CArchSleepUnix
00045 //
00046 
00047 CArchSleepUnix::CArchSleepUnix()
00048 {
00049     // do nothing
00050 }
00051 
00052 CArchSleepUnix::~CArchSleepUnix()
00053 {
00054     // do nothing
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     // prep timeout
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     // wait
00072     while (nanosleep(&t, &t) < 0)
00073         ARCH->testCancelThread();
00074 #else
00075     /* emulate nanosleep() with select() */
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 }

Generated on Thu Jun 20 2013 00:00:04 for Synergy by  doxygen 1.7.1