time-value.lisp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. (in-package :hurd-common)
  2. ;;
  3. ;; This implements a time-value class.
  4. ;; Objects of this class should be used to represent time,
  5. ;; namely the *time stat fields.
  6. ;;
  7. (defcstruct <time-value-struct>
  8. "Time value struct returned by the Mach kernel.
  9. Definition can be found at mach/time_value.h"
  10. (seconds :int)
  11. (microseconds :int))
  12. (defclass <time-value> ()
  13. ((ptr :accessor ptr
  14. :initform nil
  15. :initarg :ptr
  16. :documentation "Pointer to a time-value struct.")
  17. (seconds :initarg :seconds
  18. :accessor seconds
  19. :initform nil
  20. :documentation "Number of seconds.")
  21. (microseconds :initarg :microseconds
  22. :accessor microseconds
  23. :initform nil
  24. :documentation "Number of miliseconds."))
  25. (:documentation "Time-value class for objects that allocate pointers to time-value structures."))
  26. (defun make-time-value (&key (seconds -1) (microseconds -1))
  27. "Create a new time-value object."
  28. (declare (type fixnum seconds microseconds))
  29. (make-instance <time-value>
  30. :seconds seconds
  31. :microseconds microseconds))
  32. ;; XXX the uses of +now-time-value+ seem questionable?
  33. ;; Investigate.
  34. (defconstant +now-time-value+ (make-time-value) "Current time value.")
  35. (define-foreign-type <time-value-type> ()
  36. ()
  37. ;; XXX fix typo in CL port
  38. (:documentation "CFFI type for thye time-value-struct.")
  39. (:actual-type :pointer)
  40. (:simple-parser time-value-t))
  41. (defmethod translate-from-foreign (value (type <time-value-type>))
  42. "Translate a time-value pointer to a time-value object."
  43. (if (= -1 (foreign-slot-value value 'time-value-struct
  44. 'microseconds))
  45. +now-time-value+
  46. (make-instance <time-value>
  47. :seconds (foreign-slot-value value <time-value-struct> 'seconds)
  48. :microseconds (foreign-slot-value value <time-value-struct> 'microseconds))))
  49. (defmethod translate-to-foreign (value (type <time-value-type>))
  50. "Translate a time-value object to a foreign time-value pointer."
  51. (unless (ptr value)
  52. (let ((new-ptr (foreign-alloc <time-value-struct>)))
  53. (setf (ptr value) new-ptr)
  54. (tg:finalize value (lambda () (foreign-free new-ptr)))))
  55. (setf (foreign-slot-value (ptr value)
  56. <time-value-struct> 'seconds)
  57. (seconds value)
  58. (foreign-slot-value (ptr value)
  59. <time-value-struct> 'microseconds)
  60. (microseconds value))
  61. (ptr value))
  62. (defmethod time-value-seconds ((time <time-value>))
  63. "Returns the seconds value from a time-value 'time'."
  64. (let ((ret (seconds time)))
  65. (if (= -1 ret)
  66. (maptime-seconds *mapped-time*)
  67. ret)))
  68. (defmethod time-value-microseconds ((time <time-value>))
  69. "Returns the microseconds value from a time-value 'time'."
  70. (let ((ret (microseconds time)))
  71. (if (= -1 ret)
  72. (maptime-microseconds *mapped-time*)
  73. ret)))
  74. (defmethod time-value-equal? ((time1 <time-value>) (time2 <time-value>))
  75. "Return T if times are equal."
  76. (and (= (time-value-seconds time1)
  77. (time-value-seconds time2))
  78. (= (time-value-microseconds time1)
  79. (time-value-microseconds time2))))
  80. (defmethod time-value-newer? ((time1 <time-value>) (time2 <time-value>))
  81. "Returns T if time1 represents a newer time-value than time2."
  82. (cond
  83. ((time-value-equal? time1 +now-time-value+) t)
  84. ((> (time-value-seconds time1) (time-value-seconds time2)) t)
  85. ((< (time-value-seconds time1) (time-value-seconds time2)) nil)
  86. (t
  87. (> (time-value-microseconds time1)
  88. (time-value-microseconds time2)))))
  89. (defmethod print-object ((time <time-value>) stream)
  90. (if (time-value-equal? time +now-time-value+)
  91. (format stream "#<time-value NOW>")
  92. (format stream "#<time-value seconds=~s microseconds=~s>"
  93. (time-value-seconds time)
  94. (time-value-microseconds time))))