X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/f9a12dfac2941332998cca262de1b2c101e13fe8..ec9c71129f67c5b1c2429dbaefe23e0d19149780:/client/util_posix.c diff --git a/client/util_posix.c b/client/util_posix.c new file mode 100644 index 00000000..3e61b674 --- /dev/null +++ b/client/util_posix.c @@ -0,0 +1,63 @@ +//----------------------------------------------------------------------------- +// Copyright (C) 2010 iZsh +// +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// utilities requiring Posix library functions +//----------------------------------------------------------------------------- + +#if !defined(_WIN32) +#define _POSIX_C_SOURCE 199309L // need nanosleep() +#else +#include +#endif + +#include "util_posix.h" +#include +#include + + +// Timer functions +#if !defined (_WIN32) +#include + +static void nsleep(uint64_t n) { + struct timespec timeout; + timeout.tv_sec = n/1000000000; + timeout.tv_nsec = n%1000000000; + while (nanosleep(&timeout, &timeout) && errno == EINTR); +} + +void msleep(uint32_t n) { + nsleep(1000000 * n); +} +#endif // _WIN32 + +// a milliseconds timer for performance measurement +uint64_t msclock() { +#if defined(_WIN32) + #include + + // WORKAROUND FOR MinGW (some versions - use if normal code does not compile) + // It has no _ftime_s and needs explicit inclusion of timeb.h + #include + struct _timeb t; + _ftime(&t); + return 1000 * t.time + t.millitm; + + // NORMAL CODE (use _ftime_s) + //struct _timeb t; + //if (_ftime_s(&t)) { + // return 0; + //} else { + // return 1000 * t.time + t.millitm; + //} +#else + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return (t.tv_sec * 1000 + t.tv_nsec / 1000000); +#endif +} +