Skip to content

Commit

Permalink
Fixed timespec time calculation. Issue bkaradzic#153.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic committed May 21, 2017
1 parent aaf4948 commit e65eec4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 47 deletions.
82 changes: 41 additions & 41 deletions src/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
#if BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT
# include <windows.h>
# include <psapi.h>
#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.h> // 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 <pthread.h> // mach_port_t
# endif // BX_PLATFORM_*
Expand All @@ -43,8 +43,8 @@

# if BX_PLATFORM_ANDROID
# include <malloc.h> // mallinfo
# elif BX_PLATFORM_LINUX \
|| BX_PLATFORM_RPI \
# elif BX_PLATFORM_LINUX \
|| BX_PLATFORM_RPI \
|| BX_PLATFORM_STEAMLINK
# include <unistd.h> // syscall
# include <sys/syscall.h>
Expand Down Expand Up @@ -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_
}
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 25 additions & 6 deletions src/semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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_
}
Expand Down
15 changes: 15 additions & 0 deletions tests/os_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "test.h"
#include <bx/os.h>
#include <bx/semaphore.h>
#include <bx/timer.h>

TEST_CASE("getProcessMemoryUsed", "")
{
Expand All @@ -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);
}

0 comments on commit e65eec4

Please sign in to comment.