Add compiler macro for CREATE-SOCKET, modify %%MAKE-*-*-*-SOCKET functions and MAKE...
[iolib.git] / io.multiplex / time.lisp
blob69c68b60905f49cc234ed52c559b19bcdd47d0bb
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; time.lisp --- Various time-related functions.
4 ;;;
5 ;;; Copyright (C) 2006-2008, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;;
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
13 ;;;
14 ;;; This program is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :io.multiplex)
26 ;;;; Timeouts
28 (deftype timeout ()
29 'double-float)
31 ;;; Break a real timeout into seconds and microseconds.
32 (defun decode-timeout (timeout)
33 (assert (or (not timeout)
34 (and (typep timeout 'real)
35 (not (minusp timeout))))
36 (timeout)
37 "The timeout must be a non-negative real or NIL: ~S" timeout)
38 (typecase timeout
39 (null nil)
40 (integer (values timeout 0))
41 (real
42 (multiple-value-bind (q r) (truncate (coerce timeout 'timeout))
43 (declare (type unsigned-byte q)
44 (type timeout r))
45 (values q (the (values unsigned-byte t) (truncate (* r 1d6))))))))
47 (defun normalize-timeout (timeout)
48 (assert (and (typep timeout 'real)
49 (not (minusp timeout)))
50 (timeout)
51 "The timeout must be non-negative: ~A" timeout)
52 (coerce timeout 'timeout))
54 (defun abs-timeout (timeout)
55 (+ (osicat:get-monotonic-time) (normalize-timeout timeout)))
57 (defun min-timeout (&rest timeouts)
58 (collect-min (choose-if #'identity (scan timeouts))))