moved timeout->milisec.
[iolib.git] / io-multiplex / utils.lisp
blob47a17ab02405ffe60e12bd7045538897aeeda4c0
1 ;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 ; Copyright (C) 2006,2007 by Stelian Ionescu ;
5 ; ;
6 ; This program is free software; you can redistribute it and/or modify ;
7 ; it under the terms of the GNU General Public License as published by ;
8 ; the Free Software Foundation; either version 2 of the License, or ;
9 ; (at your option) any later version. ;
10 ; ;
11 ; This program is distributed in the hope that it will be useful, ;
12 ; but WITHOUT ANY WARRANTY; without even the implied warranty of ;
13 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;
14 ; GNU General Public License for more details. ;
15 ; ;
16 ; You should have received a copy of the GNU General Public License ;
17 ; along with this program; if not, write to the ;
18 ; Free Software Foundation, Inc., ;
19 ; 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ;
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 (in-package :io.multiplex)
24 (defun timeout->timeval (timeout tv)
25 (with-foreign-slots ((et:tv-sec et:tv-usec) tv et:timeval)
26 (multiple-value-bind (sec usec) (decode-timeout timeout)
27 (setf et:tv-sec sec
28 et:tv-usec usec))))
31 (defun timeout->timespec (timeout ts)
32 (with-foreign-slots ((et:ts-sec et:ts-nsec) ts et:timespec)
33 (multiple-value-bind (sec usec) (decode-timeout timeout)
34 (setf et:ts-sec sec
35 et:ts-nsec (* 1000 usec)))))
38 (defun timeout->milisec (timeout)
39 (if timeout
40 (multiple-value-bind (sec usec) (decode-timeout timeout)
41 (+ (* sec 1000)
42 (truncate usec 1000)))
43 -1))
46 (defmacro flags-case (mask &body clauses)
47 (let ((newm (gensym "MASK")))
48 `(let ((,newm ,mask))
49 (progn ,@(loop :for clause :in clauses
50 :collect `(when (logtest ,(let ((flags (first clause)))
51 (if (listp flags)
52 `(logior ,@flags)
53 flags))
54 ,newm)
55 ,(second clause)))))))
58 (defmacro ignore-and-print-errors (&body body)
59 `(handler-case (progn ,@body)
60 (error (error)
61 (warn "Caught a ~A: ~A, ignoring it."
62 (type-of error) error))))
65 ;; (defmacro with-restart-on-eintr (&body body)
66 ;; `(loop :for exit-p
67 ;; :while (not exit-p) :do
68 ;; (handler-case
69 ;; (progn ,@body)
70 ;; (et:unix-error-intr (err)
71 ;; (declare (ignore err))
72 ;; (setf exit-p t)))))