[plt-scheme] synchronizing pthread with MzScheme thread

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Jul 10 18:28:57 EDT 2007

At Tue, 10 Jul 2007 13:45:31 -0400, Robert Nikander wrote:
> On Mac OS X or Unix, am I correct that the way to coordinate between  
> the MzScheme thread and another OS-level thread is to make Scheme  
> wait on a file descriptor, by calling `scheme_block_until' or reading  
> from a port created with `scheme_make_fd_input_port'?
>
> The only way I know to make file descriptors suitable for this is to  
> use the `socket' and `pipe' system calls.  Is that how it's usually  
> done?

I think that's the simplest strategy.

The difficulty with other OS-level synchronization primitives is
implementing the "wakeup" interaction with MzScheme's sleep, since
MzScheme only knows how to wake up in response to file-descriptor
changes (except under Windows, where it can deal with handles). That
is, it's easy to poll a synchronization object via
scheme_block_until(), but polling doesn't work if MzScheme is asleep.

If you control all the places where MzScheme might need to wake up,
though, you can use scheme_signal_received() (which is not currently
documented; I'll fix that). The scheme_signal_received() function can
be called from any thread, and it wakes up the MzScheme thread if it's
asleep. So, if the code that posts to a semaphore (to be polled from
the MzScheme thread) also calls scheme_signal_received(), then things
will work right.

> Also, the "Inside PLT MzScheme" manual mentions `object-wait- 
> multiple'.  I assume that is an old name for `sync'?

Yes --- I'll fix that.

Matthew



Posted on the users mailing list.