chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Pipe and thread problem


From: Aaron Patterson
Subject: Re: [Chicken-users] Pipe and thread problem
Date: Thu, 25 Oct 2012 09:38:54 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

On Thu, Oct 25, 2012 at 04:12:50AM -0400, Felix wrote:
> From: Moritz Heidkamp <address@hidden>
> Subject: Re: [Chicken-users] Pipe and thread problem
> Date: Wed, 24 Oct 2012 23:10:20 +0200
> 
> > Hey Chickeneers,
> > 
> > out of curiosity I implemented (probably horribly inefficitent)
> > thread-aware replacements for open-input-file* and open-output-file*
> > (see below). While implementing them I came across something that I
> > didn't quite understand: file-read and file-write raise errors whenever
> > the underlying syscall returns -1. However, in this case I want to
> > handle some error situations (i.e. EINTR, EAGAIN and EWOULDBLOCK). The
> > only way I could find to do this is by dispatching on (errno) in the
> > excpetion handler. However it looks like (errno) is not thread-safe so
> > it might actually return a different value when my handler is run. Is
> > this observation correct? Perhaps we should mention this in errno's
> > documentation? Also, to make it possible to handle these errors I
> > suggest to add the respective errno to the condition object. This would
> > be thread-safe as the posix unit declares disable-interrupts. Thoughts?
> 
> No, "errno" is not thread-safe. Posix-related errors (see "posix-error"
> in "posix-common.scm") get attached the error-string (obtained via
> strerror(3)), also adding errno would be fine, I'd say.
> 
> I'm quite sure "##sys#custom-input-port" and
> "##sys#custom-output-port" in "posixunix.scm" are alreay doing what
> you want, and much more efficiently.

I tried changing the program to use "##sys#custom-input-port", and the
program still froze.  Here is the version with "##sys#custom-input-port"
included:

    (use srfi-18)
    (use posix)
    
    (define (produce writer)
      (thread-start!
        (lambda ()
          (print "producer started")
          (let loop ((i 0))
            (display (conc "hello" i " ") writer)
            (newline writer)
            (flush-output writer)
            (thread-sleep! 1)
            (loop (+ i 1))))))
    
    (define (consume reader)
      (thread-start!
        (lambda ()
          (print "consumer started")
          (let loop ()
            (print (read-line reader))
            (loop)))))
    
    (define (stream-test in-fd out-fd)
      (let ((reader (##sys#custom-input-port "reader" "reader" in-fd))
            (writer (##sys#custom-output-port "writer" "writer" out-fd)))
        (produce writer)
        (thread-join! (consume reader))))
    
    (call-with-values create-pipe stream-test)

I tried the same program, but with Moritz's changes, and they seemed to
work.  The code is kind of long, so I posted it here:
  
  https://gist.github.com/3953900

-- 
Aaron Patterson
http://tenderlovemaking.com/



reply via email to

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