reuse timespec value for current-seconds-monotonic

This commit is contained in:
xenia 2020-11-28 05:13:43 -05:00
parent 30fcf62d73
commit 69ae22f678
1 changed files with 5 additions and 1 deletions

View File

@ -135,13 +135,17 @@
;; things, for example on big-endian systems with 64 bit time_t and 32-bit long ;; things, for example on big-endian systems with 64 bit time_t and 32-bit long
;;; (these might not actually exist) ;;; (these might not actually exist)
(define-cstruct _timespec ([tv-sec _long] [tv-nsec _long])) (define-cstruct _timespec ([tv-sec _long] [tv-nsec _long]))
;; don't allocate a new timespec each call, just reuse the same one
;; if a thread gets preempted in this call by another thread also in this call it won't cause
;; any harmful effects
(define singleton-timespec (make-timespec 0 0))
(define *CLOCK-MONOTONIC* 1) (define *CLOCK-MONOTONIC* 1)
(define *CLOCK-MONOTONIC-RAW* 4) (define *CLOCK-MONOTONIC-RAW* 4)
(lambda () (lambda ()
(when (false? clock-gettime-call) (when (false? clock-gettime-call)
(set! clock-gettime-call (set! clock-gettime-call
(get-ffi-obj/runtime "clock_gettime" (get-libc/init) (get-ffi-obj/runtime "clock_gettime" (get-libc/init)
(_fun #:save-errno 'posix _int (out : _timespec-pointer = (make-timespec 0 0)) (_fun #:save-errno 'posix _int (out : _timespec-pointer = singleton-timespec)
-> (res : _int) -> (res : _int)
-> (if (zero? res) -> (if (zero? res)
(timespec-tv-sec out) (timespec-tv-sec out)