[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sexp oriented select

From: Christopher Cramer
Subject: Re: sexp oriented select
Date: Wed, 18 Jun 2003 19:20:40 -0500
User-agent: Mutt/1.2.5i

On Wed, Jun 18, 2003 at 10:53:43PM +0200, Ondrej Zajicek wrote:
> I am thinking about something like s-expression oriented select - it accepts
> list of ports (or file descriptors) and returns first arrived s-expression (or
> ends because of timeout) and some identifier of port sexp is from.
> Is there any clever way to implement this in guile (without reimplementing
> 'read')?

You could do it with threads, like this:

(use-modules (ice-9 threads))
(define select-sexpr
    (let (
            (m (make-mutex))
            (c (make-condition-variable))
            (e '()))
            (lambda (port)
                    (let ((x (read port)))
                        (lock-mutex m)
                        (if (null? e)
                                (set! e (cons (cons port x) e))
                                (signal-condition-variable c)
                                (unlock-mutex m))
                                (unlock-mutex m)
                                        (eof-object? x)
                                            (object->string x))))))))
        (lock-mutex m)
        (while (null? e)
            (wait-condition-variable c m))
        (let ((return e))

This code has problems (leaving threads running even after they're not
needed anymore) so you really need to use more elaborate data structures.
You couldn't use that code as is.

The other method would be storing characters in a string and then calling
read on a string port and catching exceptions etc. but I think it would
be a little wasteful if you are reading large expressions. OTOH it's the
only way to implement a timeout.

Christopher Cramer <address@hidden> <>
"People said it couldn't be that our soldiers would do such things. Now
you read worse things in the mainstream media and people don't care. We
used to say that if only people know about it, it would stop. Now they
know about it, and it hasn't stopped." - Adam Keller

reply via email to

[Prev in Thread] Current Thread [Next in Thread]