Re: Per-thread working directory and umask proposal
Marc Feeley 24 Apr 2020 12:28 UTC
> On Apr 23, 2020, at 2:55 PM, Marc Nieper-Wißkirchen <xxxxxx@nieper-wisskirchen.de> wrote:
>
>>
>> Your argument is similar to saying that we could get rid of “set!” altogether by just doing (define var (box val)) whenever we want a mutable variable. Then get rid of set-car! similarly… to just have box and set-box!. Very MLish! I view that as taking something away from my language, not the other way around!
>
> This is actually the situation with the parameter objects of
> R7RS-small where mutation is not standardized; so all you can do is to
> put boxes into the parameters. I would rather be able to set the
> parameter objects directly. (Apart from this being more direct and
> more Scheme-like, it would work on `current-input-port', etc., as
> well.)
>
> It probably depends on what would be used more, the (b) semantics or
> the (c) semantics. If the (b) semantics are at least not incorrect for
> most use cases, there's at least a way through the box thing to get to
> the (c) type of semantics.
Let me give you a real situation where the c semantics is superior and can’t be emulated with the b semantics.
Say you want to debug a program remotely, so from your program’s main thread you start a thread that implements a REPL through a network connection. The following code could be used with Gambit, but it should be easy to adapt to other Scheme’s with threads and networking:
;; remote-repl.scm
(define (remote-repl addr)
(let ((conn (open-tcp-client addr)))
(let loop ()
(display "\n> " conn)
(force-output conn)
(write (eval (read conn)) conn)
(loop))))
(thread-start!
(make-thread
(lambda ()
(remote-repl "localhost:7000"))))
In another terminal you run “nc -l 7000” to listen for the connection. Now in this terminal you can inspect through this REPL what is happening in your program.
If your program’s main thread has an issue because its binding for the current-directory parameter is not /tmp as it should be, then you can simply enter (current-directory "/tmp") in the remote REPL. It is that simple because the main thread and the REPL thread share the same parameter cells. With the b semantics you can’t implement this because current-directory is a predefined parameter and you can’t wrap it in a “box” to get the sharing semantics (more generally semantics b doesn’t scale because even if you could easily add the “box” to this parameter, you would also need to change all the places that access the current-directory in all your code and libraries, some of which are part of the runtime system that you can’t change).
My point is essentially that you shouldn’t make parameter assignment a sort of second class citizen when using threads.
Marc