#FEATURE by stefan
authorStefan Vogel <sv@exept.de>
Thu, 06 Feb 2020 23:59:25 +0100
changeset 25252 826c0e1220af
parent 25251 61b643937051
child 25253 c0422fc32176
#FEATURE by stefan class: Object added: #ensureSynchronizationSemaphore #synchronized:timeoutMs:ifBlocking: changed: #synchronized: #synchronizationSemaphore
Object.st
--- a/Object.st	Thu Feb 06 21:24:26 2020 +0100
+++ b/Object.st	Thu Feb 06 23:59:25 2020 +0100
@@ -594,6 +594,7 @@
 ! !
 
 
+
 !Object methodsFor:'accessing'!
 
 _at:index
@@ -9262,6 +9263,32 @@
 
 !Object methodsFor:'synchronized evaluation'!
 
+ensureSynchronizationSemaphore
+    "return the synchronizationSemaphore.
+     Create it save if no one exists yet"
+
+    |sema wasBlocked|
+
+    "/ instead of using another lock to assign the lock,
+    "/ we block interrupts for a short time period. This is faster.
+    wasBlocked := OperatingSystem blockInterrupts.
+
+    sema := self synchronizationSemaphore.
+    sema isNil ifTrue:[
+        sema := RecursionLock name:self className.
+        self synchronizationSemaphore:sema.
+    ].
+
+    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+    ^ sema
+
+    "
+       Object ensureSynchronizationSemaphore.
+    "
+
+    "Created: / 06-02-2020 / 18:36:56 / Stefan Vogel"
+!
+
 freeSynchronizationSemaphore
     "free synchronizationSemaphore. May be used, to save memory when
      an object is no longer used synchronized."
@@ -9286,7 +9313,8 @@
 
 synchronizationSemaphore
     "return the synchronization semaphore for myself.
-     subclasses may redefine"
+     subclasses may redefine.
+     Return nil, if none has been allocated yet."
 
     ^ SynchronizationSemaphores at:self ifAbsent:[].
 
@@ -9294,7 +9322,8 @@
       self synchronizationSemaphore
     "
 
-    "Modified: 28.1.1997 / 19:47:09 / stefan"
+    "Modified: / 28-01-1997 / 19:47:09 / stefan"
+    "Modified (comment): / 06-02-2020 / 18:39:43 / Stefan Vogel"
 !
 
 synchronizationSemaphore:aSemaphoreOrBetterARecursionLock
@@ -9316,21 +9345,11 @@
     "evaluate aBlock synchronized, i.e. use a monitor for this object;
      return the value from aBlock"
 
-    |sema wasBlocked|
+    |sema|
 
     sema := self synchronizationSemaphore.
     sema isNil ifTrue:[
-	"/ instead of using another lock to assign the lock,
-	"/ we block interrupts for a short time period. This is faster.
-	wasBlocked := OperatingSystem blockInterrupts.
-
-	sema := self synchronizationSemaphore.
-	sema isNil ifTrue:[
-	    sema := RecursionLock name:self className.
-	    self synchronizationSemaphore:sema.
-	].
-
-	wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
+        sema := self ensureSynchronizationSemaphore.
     ].
 
     ^ sema critical:aBlock.
@@ -9345,6 +9364,24 @@
     "Modified: / 09-08-2017 / 11:55:40 / cg"
     "Modified: / 29-05-2018 / 20:06:40 / Claus Gittinger"
     "Modified (comment): / 07-06-2019 / 13:50:38 / Claus Gittinger"
+    "Modified: / 06-02-2020 / 18:38:12 / Stefan Vogel"
+!
+
+synchronized:aBlock timeoutMs:timeoutMs ifBlocking:blockingBlock
+    "like synchronized:, but do not block if the lock cannot be acquired
+     within timeoutMs milliseconds.
+     Instead, return the value of blockingBlock."
+
+    |sema|
+
+    sema := self synchronizationSemaphore.
+    sema isNil ifTrue:[
+        sema := self ensureSynchronizationSemaphore.
+    ].
+
+    ^ sema critical:aBlock timeoutMs:timeoutMs ifBlocking:blockingBlock.
+
+    "Created: / 06-02-2020 / 18:43:00 / Stefan Vogel"
 ! !
 
 !Object methodsFor:'system primitives'!