On 7 January 2016 at 17:12, Eliot Miranda eliot.miranda@gmail.com wrote:
Hi Denis, Hi Clément, Hi Frank,
On Thu, Jan 7, 2016 at 5:34 AM, Clément Bera bera.clement@gmail.com wrote:
Hello,
Eliot, please, you told me you had the code and Denis is interested.
It uses 3 primitives for performance.
Forgive the delay. I thought it proper to ask permission since the code was written while I was at Qwaq. I'm attaching the code in a fairly raw state, see the attached. The code is MIT, but copyright 3DICC.
It is a plugin replacement for Squeak's Mutex, and with a little ingenuity could be a replacement for Squeak's Monitor. It is quicker because it uses three new primitives to manage entering a critical section and setting the owner, exiting the critical section and releasing the owner, and testing if a critical section, entering if the section is unowned. The use of the primitives means fewer block activations and ensure: blocks in entering and exiting the critical section, and that's the actual cause of the speed-up.
You can benchmark the code as is. Here are some results on 32-bit Spur, on my 2.2GHz Core i7
{Mutex new. Monitor new. CriticalSection new} collect: [:cs| | n | n := 0. [cs critical: [n := n + 1]. cs critical: [n := n + 1]. cs critical: [n := n
- 1]. cs critical: [n := n + 1]. cs critical: [n := n + 1].
cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1]. n ] bench]
{Mutex new. Monitor new. CriticalSection new} collect: [:cs| | n | n := 0. cs class name, ' -> ', [cs critical: [n := n + 1]. cs critical: [n := n + 1]. cs critical: [n := n
- 1]. cs critical: [n := n + 1]. cs critical: [n := n + 1].
cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1]. cs critical: [n := n - 1]. n ] bench]
#( 'Mutex -> 440,000 per second. 2.27 microseconds per run.' 'Monitor -> 688,000 per second. 1.45 microseconds per run.' 'CriticalSection -> 1,110,000 per second. 900 nanoseconds per run.')
Replacement is probably trivial; rename Mutex to OldMutex, rename CriticalSection to Mutex, recompile. But there are lots of mutexes in the system and these are potentially owned. Transforming unowned ones is trivial, but transforming owned ones is, I think, impossible. But at least in my system there are no owned mutexes or monitors.
Frank (or anyone else), would you be interested in creating a replacement for Squeak's Monitor based on CriticalSection?
It sounds like an interesting problem. I watched the thread unfold and I'm still not massively clear on what is required though? (Apologies for the delay in responding: my cup overfloweth at the moment, but should resume normal levels in a month or so.)
frank