Add a clock structure and a way to get the current clock time.
This commit is contained in:
parent
9a0872cd33
commit
3c0b452d24
1 changed files with 52 additions and 1 deletions
|
|
@ -8,7 +8,10 @@
|
|||
(:use :cl)
|
||||
(:nicknames :utils)
|
||||
(:export :restartable
|
||||
:eval-repl))
|
||||
:eval-repl
|
||||
:update-current-time :get-delta-time
|
||||
:make-clock :clock-time :clock-delta
|
||||
:update-clock :compare-clocks))
|
||||
(in-package :stoe.utils)
|
||||
|
||||
(defmacro restartable (unprotected &body body)
|
||||
|
|
@ -28,3 +31,51 @@
|
|||
(swank::default-connection))))
|
||||
(when conn
|
||||
(swank::handle-requests conn t))))
|
||||
|
||||
(defun get-current-time ()
|
||||
"Return the current time in seconds and microseconds."
|
||||
#+sbcl (sb-ext:get-time-of-day)
|
||||
#-sbcl
|
||||
(let* ((time (get-internal-real-time))
|
||||
(sec (/ time internal-time-units-per-second))
|
||||
(usec (* time (/ 1000000 internal-time-units-per-second))))
|
||||
(values sec usec)))
|
||||
|
||||
(let ((last-time (cons 0 0))
|
||||
(current-time (cons 0 0)))
|
||||
|
||||
(defun update-current-time ()
|
||||
"Update the cached time in seconds and microseconds."
|
||||
(setf (car last-time) (car current-time))
|
||||
(setf (cdr last-time) (cdr current-time))
|
||||
(multiple-value-bind (sec usec) (get-current-time)
|
||||
(setf (car current-time) sec)
|
||||
(setf (cdr current-time) usec)))
|
||||
|
||||
(defun get-delta-time ()
|
||||
"Return the difference between the last two cached timers."
|
||||
(+ (* (- (car current-time) (car last-time)) 1000000)
|
||||
(- (cdr current-time) (cdr last-time)))))
|
||||
|
||||
(defstruct (clock (:constructor %make-clock))
|
||||
(time 0)
|
||||
(last-time 0)
|
||||
(scale 1.0)
|
||||
(paused nil))
|
||||
|
||||
(defun make-clock (&optional (time 0) (scale 1.0) (paused nil))
|
||||
"Create a new clock instance with specified parameters or using reasonable defaults."
|
||||
(%make-clock :time time :last-time time :scale scale :paused paused))
|
||||
|
||||
(defun update-clock (clock &optional delta-time)
|
||||
"Update clock using `sec' and `usec' values passed as parameter."
|
||||
(unless (clock-paused clock)
|
||||
(setf (clock-last-time clock) (clock-time clock))
|
||||
(setf (clock-time clock) (* (or delta-time (get-delta-time)) (clock-scale clock)))))
|
||||
|
||||
(defun clock-delta (clock)
|
||||
(- (clock-time clock) (clock-last-time clock)))
|
||||
|
||||
(defun compare-clocks (clock1 clock2)
|
||||
"Return the difference between `clock1' and `clock2'."
|
||||
(- (clock-time clock1) (clock-time clock2)))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue