diff --git a/src/os.cpp b/src/os.cpp index 291b281..d18384b 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -15,23 +15,23 @@ #if BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT # include # include -#elif BX_PLATFORM_ANDROID \ +#elif BX_PLATFORM_ANDROID \ || BX_PLATFORM_EMSCRIPTEN \ - || BX_PLATFORM_BSD \ - || BX_PLATFORM_HURD \ - || BX_PLATFORM_IOS \ - || BX_PLATFORM_LINUX \ - || BX_PLATFORM_NACL \ - || BX_PLATFORM_OSX \ - || BX_PLATFORM_PS4 \ - || BX_PLATFORM_RPI \ + || BX_PLATFORM_BSD \ + || BX_PLATFORM_HURD \ + || BX_PLATFORM_IOS \ + || BX_PLATFORM_LINUX \ + || BX_PLATFORM_NACL \ + || BX_PLATFORM_OSX \ + || BX_PLATFORM_PS4 \ + || BX_PLATFORM_RPI \ || BX_PLATFORM_STEAMLINK # include // sched_yield -# if BX_PLATFORM_BSD \ - || BX_PLATFORM_IOS \ +# if BX_PLATFORM_BSD \ + || BX_PLATFORM_IOS \ || BX_PLATFORM_NACL \ - || BX_PLATFORM_OSX \ - || BX_PLATFORM_PS4 \ + || BX_PLATFORM_OSX \ + || BX_PLATFORM_PS4 \ || BX_PLATFORM_STEAMLINK # include // mach_port_t # endif // BX_PLATFORM_* @@ -43,8 +43,8 @@ # if BX_PLATFORM_ANDROID # include // mallinfo -# elif BX_PLATFORM_LINUX \ - || BX_PLATFORM_RPI \ +# elif BX_PLATFORM_LINUX \ + || BX_PLATFORM_RPI \ || BX_PLATFORM_STEAMLINK # include // syscall # include @@ -74,8 +74,8 @@ namespace bx BX_UNUSED(_ms); debugOutput("sleep is not implemented"); debugBreak(); #else - timespec req = {(time_t)_ms/1000, (long)((_ms%1000)*1000000)}; - timespec rem = {0, 0}; + timespec req = { (time_t)_ms/1000, (long)( (_ms%1000)*1000000) }; + timespec rem = { 0, 0 }; ::nanosleep(&req, &rem); #endif // BX_PLATFORM_ } @@ -175,9 +175,9 @@ namespace bx #if BX_PLATFORM_WINDOWS return (void*)::LoadLibraryA(_filePath); #elif BX_PLATFORM_EMSCRIPTEN \ - || BX_PLATFORM_NACL \ - || BX_PLATFORM_PS4 \ - || BX_PLATFORM_XBOXONE \ + || BX_PLATFORM_NACL \ + || BX_PLATFORM_PS4 \ + || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT BX_UNUSED(_filePath); return NULL; @@ -191,9 +191,9 @@ namespace bx #if BX_PLATFORM_WINDOWS ::FreeLibrary( (HMODULE)_handle); #elif BX_PLATFORM_EMSCRIPTEN \ - || BX_PLATFORM_NACL \ - || BX_PLATFORM_PS4 \ - || BX_PLATFORM_XBOXONE \ + || BX_PLATFORM_NACL \ + || BX_PLATFORM_PS4 \ + || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT BX_UNUSED(_handle); #else @@ -206,9 +206,9 @@ namespace bx #if BX_PLATFORM_WINDOWS return (void*)::GetProcAddress( (HMODULE)_handle, _symbol); #elif BX_PLATFORM_EMSCRIPTEN \ - || BX_PLATFORM_NACL \ - || BX_PLATFORM_PS4 \ - || BX_PLATFORM_XBOXONE \ + || BX_PLATFORM_NACL \ + || BX_PLATFORM_PS4 \ + || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT BX_UNUSED(_handle, _symbol); return NULL; @@ -224,7 +224,7 @@ namespace bx bool result = len != 0 && len < *_inOutSize; *_inOutSize = len; return result; -#elif BX_PLATFORM_PS4 \ +#elif BX_PLATFORM_PS4 \ || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT BX_UNUSED(_name, _out, _inOutSize); @@ -253,7 +253,7 @@ namespace bx { #if BX_PLATFORM_WINDOWS ::SetEnvironmentVariableA(_name, _value); -#elif BX_PLATFORM_PS4 \ +#elif BX_PLATFORM_PS4 \ || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT BX_UNUSED(_name, _value); @@ -266,7 +266,7 @@ namespace bx { #if BX_PLATFORM_WINDOWS ::SetEnvironmentVariableA(_name, NULL); -#elif BX_PLATFORM_PS4 \ +#elif BX_PLATFORM_PS4 \ || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT BX_UNUSED(_name); @@ -277,7 +277,7 @@ namespace bx int chdir(const char* _path) { -#if BX_PLATFORM_PS4 \ +#if BX_PLATFORM_PS4 \ || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT BX_UNUSED(_path); @@ -291,7 +291,7 @@ namespace bx char* pwd(char* _buffer, uint32_t _size) { -#if BX_PLATFORM_PS4 \ +#if BX_PLATFORM_PS4 \ || BX_PLATFORM_XBOXONE \ || BX_PLATFORM_WINRT BX_UNUSED(_buffer, _size); @@ -432,16 +432,16 @@ namespace bx } bool ok = !!CreateProcessA(_argv[0] - , temp - , NULL - , NULL - , false - , 0 - , NULL - , NULL - , &si - , &pi - ); + , temp + , NULL + , NULL + , false + , 0 + , NULL + , NULL + , &si + , &pi + ); if (ok) { return pi.hProcess; diff --git a/src/semaphore.cpp b/src/semaphore.cpp index b4369ff..dae780d 100644 --- a/src/semaphore.cpp +++ b/src/semaphore.cpp @@ -52,6 +52,28 @@ namespace bx #if BX_PLATFORM_POSIX + uint64_t toNs(const timespec& _ts) + { + return _ts.tv_sec*UINT64_C(1000000000) + _ts.tv_nsec; + } + + void toTimespecNs(timespec& _ts, uint64_t _nsecs) + { + _ts.tv_sec = _nsecs/UINT64_C(1000000000); + _ts.tv_nsec = _nsecs%UINT64_C(1000000000); + } + + void toTimespecMs(timespec& _ts, int32_t _msecs) + { + toTimespecNs(_ts, _msecs*1000000); + } + + void add(timespec& _ts, int32_t _msecs) + { + uint64_t ns = toNs(_ts); + toTimespecNs(_ts, ns + _msecs*1000000); + } + # if BX_CONFIG_SEMAPHORE_PTHREAD Semaphore::Semaphore() { @@ -133,8 +155,7 @@ namespace bx else { timespec ts; - ts.tv_sec = _msecs/1000; - ts.tv_nsec = (_msecs%1000)*1000; + toTimespecMs(ts, _msecs); while (0 == result && 0 >= si->m_count) @@ -145,8 +166,7 @@ namespace bx # else timespec ts; clock_gettime(CLOCK_REALTIME, &ts); - ts.tv_sec += _msecs/1000; - ts.tv_nsec += (_msecs%1000)*1000; + add(ts, _msecs); while (0 == result && 0 >= si->m_count) @@ -226,8 +246,7 @@ namespace bx timespec ts; clock_gettime(CLOCK_REALTIME, &ts); - ts.tv_sec += _msecs/1000; - ts.tv_nsec += (_msecs%1000)*1000; + add(ts, _msecs); return 0 == sem_timedwait(&si->m_handle, &ts); # endif // BX_PLATFORM_ } diff --git a/tests/os_test.cpp b/tests/os_test.cpp index e6aeb31..ebd5ae5 100644 --- a/tests/os_test.cpp +++ b/tests/os_test.cpp @@ -5,6 +5,8 @@ #include "test.h" #include +#include +#include TEST_CASE("getProcessMemoryUsed", "") { @@ -18,3 +20,16 @@ TEST_CASE("getTempPath", "") uint32_t len = BX_COUNTOF(tmpDir); REQUIRE(bx::getTempPath(tmpDir, &len) ); } + +TEST_CASE("semaphore_timeout", "") +{ + bx::Semaphore sem; + + int64_t start = bx::getHPCounter(); + bool ok = sem.wait(900); + int64_t elapsed = bx::getHPCounter() - start; + int64_t frequency = bx::getHPFrequency(); + double ms = double(elapsed) / double(frequency) * 1000; + printf("%f\n", ms); + REQUIRE(!ok); +}