Better %SYS-GETTID.
[iolib.git] / base / time.lisp
blob93e607e06de09c034cf065c9b1d92ea9d504ba75
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- Time utils.
4 ;;;
6 (in-package :iolib.base)
8 (deftype timeout ()
9 'double-float)
11 ;;; Break a real timeout into seconds and microseconds.
12 (defun decode-timeout (timeout)
13 (assert (or (not timeout)
14 (and (typep timeout 'real)
15 (not (minusp timeout))))
16 (timeout)
17 "The timeout must be a non-negative real or NIL: ~S" timeout)
18 (typecase timeout
19 (null nil)
20 (integer (values timeout 0))
21 (real
22 (multiple-value-bind (q r) (truncate (coerce timeout 'timeout))
23 (declare (type unsigned-byte q)
24 (type timeout r))
25 (values q (the (values unsigned-byte t) (truncate (* r 1d6))))))))
27 (defun normalize-timeout (timeout)
28 (assert (and (typep timeout 'real)
29 (not (minusp timeout)))
30 (timeout)
31 "The timeout must be non-negative: ~A" timeout)
32 (coerce timeout 'timeout))
34 (defun clamp-timeout (timeout &optional (min 0) (max most-positive-fixnum))
35 (clamp (or timeout most-positive-fixnum)
36 (if min (max min 0) 0) (or max most-positive-fixnum)))