On 4/7/2013 7:58 PM, John Nagle wrote:
> On 4/7/2013 11:25 AM, Thomas Kappler wrote:
>> Hi gophers,
>>
>> Tony Hoare's paper 1978 paper "Communicating sequential processes" is one
>> of the great influential papers in Computer Science. I had heard about it
>> at uni, forgot about it, then re-discovered it as one of the inspirations
>> for Go. The best way to really grok something is to do it, so I implemented
>> all examples from Hoare's paper in Go. This code is supposed to be purely
>> educational and is not intended for practical use.
>>
>> The godoc has more info and comments for each section:
>>
http://godoc.org/github.com/thomas11/csp. Clicking the headers takes you
>> directly to the code.
>>
>> -- Thomas
>
> P and V, and bounded buffers, predate Hoare's paper. They're from
> earlier work by Dijkstra from the 1960s. See
>
>
http://www.cs.utexas.edu/~EWD/transcriptions/EWD01xx/EWD123.html
>
> Implementing P and V on top of channels is kind of backwards. Channels
> themselves are usually implemented in terms of P and V.
....
> Go's "sync" primitives (
http://golang.org/pkg/sync/) are perhaps
> too high level. Everything there could be implemented in terms
> of P, V, and PMAY (the non-blocking form). Get those right and
> make them fast, and everything else falls into place. Many
> modern implementations botch this by making a system call for
> a non-blocking semaphore lock.
That's the real point I'm making here. P and V, if implemented
properly, are both very fast and provide a basis for all the other
operations, including bounded buffers.
Optimal P and V implementations are platform-dependent. You
can build them using atomic increment, compare and swap, or test
and set. But you need to choose one of those that's very fast
on the target platform, and this varies with target platform.
They often require instructions which compilers usually don't
emit, so they're usually written in assembler. But not
many instructions; these are short, very carefully written bits of
assembler.
After a V, even one that doesn't wake up anything, it's necessary
on some platforms to perform a memory fence operation to sync caches.
Once you have P and V, you're above the machine level,
don't have to worry about that stuff again, and can write portably
in a high level language. Get P and V right, and critical section
overhead is very low. POSIX or Win32 locking primitives have
much higher overhead.
John Nagle