3
0
Fork 0
forked from mirrors/nixpkgs
nixpkgs/pkgs/development/libraries/libsnark/darwin-fix-clock-gettime.patch

42 lines
1.5 KiB
Diff
Raw Normal View History

2018-01-20 05:04:02 +00:00
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
}