Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Thin locks: featherweight synchronization also for Smalltalk by Jan Vrany

76 views

Published on

Traditional and still prevalent concurrency model is based on threads and shared mutable state. Smalltalk is no different in that respect, having processes that can run concurrently virtually "ever since". The traditional solution to prevent race condition is to use locks for synchronization. Many actions must be properly synchronized, even the simplest things like a Transcript or an access to a method source code. In this presentation I will talk about motivation, use and actual implementation of thinlocks - a fast locking mechanism invented for Java - in scope of Smalltalk.

Published in: Technology
  • Login to see the comments

  • Be the first to like this

Thin locks: featherweight synchronization also for Smalltalk by Jan Vrany

  1. 1. Thin locks: featherweight synchronization also for Smalltalk Jan Vraný
  2. 2. Benchmark # Enter/Exit # Contentions % Contentions JVM boot 6 808 72 1.0% Groovy 3 + 4 102 835 5 484 5.3% Tomcat startup 703 267 12992 1.8%
  3. 3. close self closed ifTrue: [ ^ self ]. File primClose: handle. self unregister. handle := nil
  4. 4. close self closed ifTrue: [ ^ self ]. File primClose: handle. self unregister. handle := nil close self closed ifTrue: [ ^ self ]. File primClose: handle. self unregister. handle := nil Thread A Thread B
  5. 5. close self closed ifTrue: [ ^ self ]. File primClose: handle. self unregister. handle := nil close self closed ifTrue: [ ^ self ]. File primClose: handle. self unregister. handle := nil Thread A Thread B open handle := ... Thread C
  6. 6. The Deadlock Empire attacks! The war has begun. https://deadlockempire.github.io/
  7. 7. Thin locks: featherweight synchronization for Java David F. Bacon, Ravi Konuru, Chet Murthy, Mauricio Serrano IBM T.J. Watson Research Center
  8. 8. unlocked → locked locked twice contention 0 1000 2000 3000 4000 5000 6000 St/X 8.x.x (w/ thin locks) St/X 6.x.x Pharo 6.1
  9. 9. 10 0 031/63 unlocked
  10. 10. 1123 0 031/63 locked once by thread 123
  11. 11. 1123 1 031/63 locked twice by thread 123
  12. 12. 00xCAFEAFFE 031/63 locked, contended
  13. 13. acquireWithTimeoutMs: timeout %{ /* NOCONTEXT */ if ( stxThinLock( &(__INST(process)) ) { return true; } %}. process class == SmallInteger ifTrue:[ self inflate ]. ^ super acquireWithTimeoutMs: timeout
  14. 14. static inline bool stxThinLock(UINT* lockword) { if (CAS(lockword, UNLOCKED, LOCKED_ONCE)) { return 1; } if (CAS(lockword, LOCKED_ONCE, LOCKED_TWICE)) { return 1; } … #define UNLOCKED 0x1 #define LOCKED_ONCE (activeProcId) #define LOCKED_TWICE (activeProcId + 2)
  15. 15. … UINT expected = *lockword; if ((expected & PID_MASK) == activeProcId)&& (expected & CNT_MASK) < CNT_MAX)) { if (CAS(lockword, expected, expected + 2)) { return 1; } } return 0; }
  16. 16. Thin locks: featherweight synchronization for Java David F. Bacon, Ravi Konuru, Chet Murthy & Mauricio Serrano

×