Windows port. Lots of rough edges, passes the most important tests.
[iolib.git] / io-multiplex / utils.lisp
bloba4788336073ead5f2e374c770ba5417a70e5f328
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; utils.lisp --- Miscellaneous utilities.
4 ;;;
5 ;;; Copyright (C) 2006-2007, 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 #-windows
27 (defun timeout->timeval (timeout tv)
28 (with-foreign-slots ((nix::sec nix::usec) tv nix::timeval)
29 (multiple-value-bind (sec usec) (decode-timeout timeout)
30 (setf nix::sec sec
31 nix::usec usec))))
33 #-windows
34 (defun timeout->timespec (timeout ts)
35 (with-foreign-slots ((nix::sec nix::nsec) ts nix::timespec)
36 (multiple-value-bind (sec usec) (decode-timeout timeout)
37 (setf nix::sec sec
38 nix::nsec (* 1000 usec)))))
40 (defun timeout->milisec (timeout)
41 (if timeout
42 (multiple-value-bind (sec usec) (decode-timeout timeout)
43 (+ (* sec 1000)
44 (truncate usec 1000)))
45 -1))
47 (defmacro flags-case (mask &body clauses)
48 (once-only (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 ,mask)
55 ,(second clause))))))
57 (defmacro ignore-and-print-errors (&body body)
58 `(handler-case (progn ,@body)
59 (error (error)
60 (warn "Caught a ~A: ~A, ignoring it."
61 (type-of error) error))))