Minor fixes in SHUTDOWN and %RECEIVE-FROM.
[iolib.git] / io.multiplex / utils.lisp
blobc8ad9fc8cea4566c129cd75c9b6ef3695bd292c9
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; utils.lisp --- Miscellaneous utilities.
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 (defun timeout->timeval (timeout tv)
27 (with-foreign-slots ((sec usec) tv timeval)
28 (multiple-value-bind (%sec %usec) (decode-timeout timeout)
29 (setf sec %sec
30 usec %usec))))
32 (defun timeout->timespec (timeout ts)
33 (with-foreign-slots ((sec nsec) ts timespec)
34 (multiple-value-bind (%sec %usec) (decode-timeout timeout)
35 (setf sec %sec
36 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))
45 (defmacro flags-case (mask &body clauses)
46 (once-only (mask)
47 `(progn ,@(loop :for clause :in clauses
48 :collect `(when (logtest ,(let ((flags (first clause)))
49 (if (listp flags)
50 `(logior ,@flags)
51 flags))
52 ,mask)
53 ,(second clause))))))
55 (defmacro ignore-and-print-errors (&body body)
56 `(handler-case (locally ,@body)
57 (error (error)
58 (warn "Caught a ~A: ~A, ignoring it."
59 (type-of error) error))))