summary refs log tree commit diff
path: root/pkgs/development/libraries/libsnark/darwin-fix-clock-gettime.patch
blob: 2eee84d1c4b84a10476f5a9188c9d7b6b82707b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Adapted from https://github.com/zcash/libsnark/pull/10

diff --git a/depends/libff/libff/common/profiling.cpp b/depends/libff/libff/common/profiling.cpp
index f2a1985..319149c 100755
--- a/depends/libff/libff/common/profiling.cpp
+++ b/depends/libff/libff/common/profiling.cpp
@@ -27,6 +27,13 @@
 #include <proc/readproc.h>
 #endif
 
+#ifdef __MACH__
+#include <time.h>
+#include <sys/time.h>
+#include <mach/clock.h>
+#include <mach/mach.h>
+#endif
+
 namespace libff {
 
 long long get_nsec_time()
@@ -42,10 +49,20 @@ long long get_nsec_cpu_time()
 	return 0;
 #else
     ::timespec ts;
+#ifdef __MACH__
+    clock_serv_t cclock;
+    mach_timespec_t mts;
+    host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
+    clock_get_time(cclock, &mts);
+    mach_port_deallocate(mach_task_self(), cclock);
+    ts.tv_sec = mts.tv_sec;
+    ts.tv_nsec = mts.tv_nsec;
+#else
     if ( ::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) )
         throw ::std::runtime_error("clock_gettime(CLOCK_PROCESS_CPUTIME_ID) failed");
         // If we expected this to work, don't silently ignore failures, because that would hide the problem and incur an unnecessarily system-call overhead. So if we ever observe this exception, we should probably add a suitable #ifdef .
         //TODO: clock_gettime(CLOCK_PROCESS_CPUTIME_ID) is not supported by native Windows. What about Cygwin? Should we #ifdef on CLOCK_PROCESS_CPUTIME_ID or on __linux__?
+#endif
     return ts.tv_sec * 1000000000ll + ts.tv_nsec;
 #endif
 }