[M3devel] Latest pthreads attempt

Tony Hosking hosking at cs.purdue.edu
Wed Aug 13 21:40:32 CEST 2014

On Aug 12, 2014, at 6:12 PM, mika at async.caltech.edu wrote:

> Tony,
> I've been reading a bit of the pthread code and I have a question...
> What if one were to write a ThreadPThread that is a very thin veneer
> over pthreads?  What goes wrong?  That is, simply translate Modula-3
> calls Thread.Acquire to pthread_lock(...), Modula-3 condition variables
> to pthread condition variables, etc.

Way back in the dim dark days of early pthreads for Modula-3 this is exactly what we had.  But it turned out to make a lot of the Modula-3 thread semantics difficult/impossible to implement.  I’d have to trawl through the logs to recall the specifics.  But it would still make fork impossible to implement because threads could have an arbitrary set of locks locked at the fork.

> I know Alerted is a bit different but it's cooperative: both the waiter
> and alerter know they are going to be Alerted (so it could be less 
> efficient and have extra data structures).

I seem to recall that was one of the issues.

> Would there be issues with the garbage collector?  Some inefficiency?

Hmm.  Not sure.  The GC is fairly agnostic so long as allocation is not interruptible (hence the inCritical flag in the heapState field).

> I think I am mainly worried about the presence of "GetActivation()"
> in critical places like LockMutex and UnlockMutex.  Maybe it's not a
> big deal anymore...(it used to be a real drag at least on FreeBSD
> because it involved a system call).

Tough to get away with that if we wrap pthread mutexes like we do.  But yes, much cheaper on modern thread implementations.  And wrapping allows us to go to a thin-lock implementation using CAS (acquire of an uncontended mutex is simply CAS(ADR(m.holder), NIL, self).  A contended mutex can be detected by setting a low bit in the holder field.  And even better, we can implement biased locking, wherein an uncontended lock is biased towards one particular thread which can lock it without CAS.  Revoking the bias requires stopping the thread holding the bias, clearing it, and resuming.

Need more time...

>     Mika

More information about the M3devel mailing list