| 1 | /* | 
|---|
| 2 | * synergy -- mouse and keyboard sharing utility | 
|---|
| 3 | * Copyright (C) 2002 Chris Schoeneman | 
|---|
| 4 | * | 
|---|
| 5 | * This package is free software you can redistribute it and/or | 
|---|
| 6 | * modify it under the terms of the GNU General Public License | 
|---|
| 7 | * found in the file COPYING that should have accompanied this file. | 
|---|
| 8 | * | 
|---|
| 9 | * This package is distributed in the hope that it will be useful, | 
|---|
| 10 | * but WITHOUT ANY WARRANTY without even the implied warranty of | 
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 12 | * GNU General Public License for more details. | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | #include "CArchSleepUnix.h" | 
|---|
| 16 | #include "CArch.h" | 
|---|
| 17 | #if TIME_WITH_SYS_TIME | 
|---|
| 18 | #       include <sys/time.h> | 
|---|
| 19 | #       include <time.h> | 
|---|
| 20 | #else | 
|---|
| 21 | #       if HAVE_SYS_TIME_H | 
|---|
| 22 | #               include <sys/time.h> | 
|---|
| 23 | #       else | 
|---|
| 24 | #               include <time.h> | 
|---|
| 25 | #       endif | 
|---|
| 26 | #endif | 
|---|
| 27 | #if !HAVE_NANOSLEEP | 
|---|
| 28 | #       if HAVE_SYS_SELECT_H | 
|---|
| 29 | #               include <sys/select.h> | 
|---|
| 30 | #       endif | 
|---|
| 31 | #       if HAVE_SYS_TYPES_H | 
|---|
| 32 | #               include <sys/types.h> | 
|---|
| 33 | #       endif | 
|---|
| 34 | #       if HAVE_UNISTD_H | 
|---|
| 35 | #               include <unistd.h> | 
|---|
| 36 | #       endif | 
|---|
| 37 | #endif | 
|---|
| 38 |  | 
|---|
| 39 | // | 
|---|
| 40 | // CArchSleepUnix | 
|---|
| 41 | // | 
|---|
| 42 |  | 
|---|
| 43 | CArchSleepUnix::CArchSleepUnix() | 
|---|
| 44 | { | 
|---|
| 45 | // do nothing | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | CArchSleepUnix::~CArchSleepUnix() | 
|---|
| 49 | { | 
|---|
| 50 | // do nothing | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | void | 
|---|
| 54 | CArchSleepUnix::sleep(double timeout) | 
|---|
| 55 | { | 
|---|
| 56 | ARCH->testCancelThread(); | 
|---|
| 57 | if (timeout < 0.0) { | 
|---|
| 58 | return; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | #if HAVE_NANOSLEEP | 
|---|
| 62 | // prep timeout | 
|---|
| 63 | struct timespec t; | 
|---|
| 64 | t.tv_sec  = (long)timeout; | 
|---|
| 65 | t.tv_nsec = (long)(1.0e+9 * (timeout - (double)t.tv_sec)); | 
|---|
| 66 |  | 
|---|
| 67 | // wait | 
|---|
| 68 | while (nanosleep(&t, &t) < 0) | 
|---|
| 69 | ARCH->testCancelThread(); | 
|---|
| 70 | #else | 
|---|
| 71 | /* emulate nanosleep() with select() */ | 
|---|
| 72 | double startTime = ARCH->time(); | 
|---|
| 73 | double timeLeft  = timeout; | 
|---|
| 74 | while (timeLeft > 0.0) { | 
|---|
| 75 | struct timeval timeout2; | 
|---|
| 76 | timeout2.tv_sec  = static_cast<int>(timeLeft); | 
|---|
| 77 | timeout2.tv_usec = static_cast<int>(1.0e+6 * (timeLeft - | 
|---|
| 78 | timeout2.tv_sec)); | 
|---|
| 79 | select((SELECT_TYPE_ARG1)  0, | 
|---|
| 80 | SELECT_TYPE_ARG234 NULL, | 
|---|
| 81 | SELECT_TYPE_ARG234 NULL, | 
|---|
| 82 | SELECT_TYPE_ARG234 NULL, | 
|---|
| 83 | SELECT_TYPE_ARG5   &timeout2); | 
|---|
| 84 | ARCH->testCancelThread(); | 
|---|
| 85 | timeLeft = timeout - (ARCH->time() - startTime); | 
|---|
| 86 | } | 
|---|
| 87 | #endif | 
|---|
| 88 | } | 
|---|