diff --git a/timer.lisp b/timer.lisp index 568f868..49d8675 100644 --- a/timer.lisp +++ b/timer.lisp @@ -30,11 +30,8 @@ (if (numberp time) ;; time is not bogus, make a timeval struct and add the event to the ;; loop for delayed processing. - (multiple-value-bind (time-sec time-usec) (split-usec-time time) - (make-foreign-type (time-c (le::cffi-type le::timeval)) - (('le::tv-sec time-sec) - ('le::tv-usec time-usec)) - (le:event-add ev time-c))) + (with-struct-timeval time-c time + (le:event-add ev time-c)) ;; there was no time specified (or it wasn't a number), so fire up the ;; event to be processed with no delay (le:event-active ev 0 0)))) diff --git a/util.lisp b/util.lisp index 24f302b..6ab8dfc 100644 --- a/util.lisp +++ b/util.lisp @@ -47,6 +47,7 @@ #:clear-pointer-data #:free-pointer-data + #:with-struct-timeval #:split-usec-time #:append-array @@ -234,6 +235,14 @@ (when (cffi:pointerp pointer) (cffi:foreign-free pointer)))))) +(defmacro with-struct-timeval (var seconds &rest body) + "Convert seconds to a valid struct timeval C data type." + `(multiple-value-bind (time-sec time-usec) (split-usec-time ,seconds) + (make-foreign-type (,var (le::cffi-type le::timeval)) + (('le::tv-sec time-sec) + ('le::tv-usec time-usec)) + ,@body))) + (defun split-usec-time (time-s) "Given a second value, ie 3.67, return the number of seconds as the first value and the number of usecs for the second value."