Skip to content

Commit 21a4fd0

Browse files
authored
Merge pull request #2 from StrangeLoopGames/simon/nanoseconds
Nanoseconds
2 parents aad99f8 + a6e9f1b commit 21a4fd0

14 files changed

Lines changed: 117 additions & 34 deletions

NativeTime.Plugin/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ endif()
2727
if (APPLE)
2828
set(CMAKE_C_FLAGS "-x objective-c")
2929
add_compile_options("-dynamiclib")
30-
target_sources(NativeTimeNative PRIVATE Metal.mm)
3130
elseif(UNIX)
3231
add_compile_definitions(UNITY_LINUX=1)
3332
endif()

NativeTime.Plugin/Linux.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "pch.h"
2+
3+
#ifdef UNITY_LINUX
4+
5+
#include "NativeTime.h"
6+
#include "IUnityInterface.h"
7+
8+
const int NsToTicks = 100;
9+
const int SecToTicks = 10000000;
10+
const int SecondsToNanoSeconds = 1000000000;
11+
12+
struct timespec resolution;
13+
14+
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API Init()
15+
{
16+
clock_getres(CLOCK_MONOTONIC_RAW, &resolution);
17+
}
18+
19+
extern "C" uint64_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetTimestampNs()
20+
{
21+
struct timespec time;
22+
clock_gettime(CLOCK_MONOTONIC_RAW, &time);
23+
return (static_cast<uint64_t>(time.tv_sec) * SecondsToNanoSeconds) + static_cast<uint64_t>(time.tv_nsec);
24+
}
25+
26+
27+
#endif

NativeTime.Plugin/Metal.mm

Lines changed: 0 additions & 8 deletions
This file was deleted.

NativeTime.Plugin/NativeTime.Plugin.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
</ItemGroup>
193193
<ItemGroup>
194194
<ClInclude Include="framework.h" />
195+
<ClInclude Include="Linux.cpp" />
195196
<ClInclude Include="pch.h" />
196197
<ClInclude Include="PlatformBase.h" />
197198
<ClInclude Include="PluginAPI\IUnityEventQueue.h" />

NativeTime.Plugin/NativeTime.Plugin.vcxproj.filters

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<Filter>PluginAPI</Filter>
4242
</ClInclude>
4343
<ClInclude Include="PlatformBase.h" />
44+
<ClInclude Include="Linux.cpp" />
4445
</ItemGroup>
4546
<ItemGroup>
4647
<None Include="cpp.hint" />

NativeTime.Plugin/NativeTime.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@
1414
#define NATIVETIME_API __declspec(dllimport)
1515
#endif
1616

17-
extern "C" int64_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetTimestamp();
17+
const int SecondsToMicroSeconds = 1000000;
18+
const int SecondsToNanoSeconds = 1000000000;
19+
20+
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API Init();
21+
extern "C" uint64_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetTimestampNs();

NativeTime.Plugin/Osx.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
#include "IUnityInterface.h"
77

88

9-
extern "C" int64_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetTimestamp()
9+
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API Init()
1010
{
11-
return 0;
11+
}
12+
13+
extern "C" uint64_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetTimestampNs()
14+
{
15+
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
1216
}
1317

1418

NativeTime.Plugin/Windows.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55
#include "NativeTime.h"
66
#include "IUnityInterface.h"
77

8-
#include <windows.h>
9-
#include <profileapi.h>
8+
static uint64_t s_frequency = 0;
109

11-
static LARGE_INTEGER s_frequency = LARGE_INTEGER();
12-
13-
extern "C" int64_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetTimestamp()
10+
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API Init()
1411
{
15-
if (s_frequency.QuadPart == 0)
16-
{
17-
QueryPerformanceFrequency(&s_frequency);
18-
}
12+
LARGE_INTEGER frequency;
13+
QueryPerformanceFrequency(&frequency);
14+
s_frequency = frequency.QuadPart;
15+
}
1916

17+
extern "C" uint64_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetTimestampNs()
18+
{
2019
LARGE_INTEGER time;
2120
QueryPerformanceCounter(&time);
22-
time.QuadPart *= 10000000;
23-
time.QuadPart /= s_frequency.QuadPart;
24-
return time.QuadPart;
21+
uint64_t elapsed = time.QuadPart;
22+
23+
return elapsed * SecondsToNanoSeconds / s_frequency;
2524
}
2625

2726
#endif

NativeTime.Plugin/pch.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,21 @@
77
#ifndef PCH_H
88
#define PCH_H
99

10-
// add headers that you want to pre-compile here
11-
#include "framework.h"
1210
#include "PlatformBase.h"
1311

12+
#ifdef UNITY_WIN
13+
#include <windows.h>
14+
#include <profileapi.h>
15+
16+
#elif UNITY_OSX
17+
#include <time.h>
18+
19+
#elif UNITY_LINUX
20+
#define _POSIX_C_SOURCE 199309L
21+
#include <time.h>
22+
23+
#else
24+
#error "Unsupported platform"
25+
#endif
26+
1427
#endif //PCH_H

NativeTime.sln

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ EndProject
1212
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NativeTime", "NativeTime", "{A7E3F065-2DE0-4BE2-B580-7D447C05BCDB}"
1313
ProjectSection(SolutionItems) = preProject
1414
.gitignore = .gitignore
15+
appveyor.yml = appveyor.yml
16+
CMakeLists.txt = CMakeLists.txt
17+
LICENSE = LICENSE
1518
package.json = package.json
19+
README.md = README.md
1620
EndProjectSection
1721
EndProject
1822
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NativeTime.Plugin", "NativeTime.Plugin\NativeTime.Plugin.vcxproj", "{A20656BB-F143-45E8-A067-58E7A1EE36E1}"

0 commit comments

Comments
 (0)