Skip to content

Commit

Permalink
Add `with-struct-timeval'
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Danjou <julien@danjou.info>
  • Loading branch information
jd committed Jan 14, 2013
1 parent 46727f3 commit 16576a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 2 additions & 5 deletions timer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down
9 changes: 9 additions & 0 deletions util.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#:clear-pointer-data
#:free-pointer-data

#:with-struct-timeval
#:split-usec-time

#:append-array
Expand Down Expand Up @@ -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."
Expand Down

0 comments on commit 16576a3

Please sign in to comment.