You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/01/24 13:17:21 UTC

[isis] branch ISIS-1846_internal_utils updated: ISIS-1846 thread-safe (atomic) writes to _Context (singleton-store)

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch ISIS-1846_internal_utils
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/ISIS-1846_internal_utils by this push:
     new 8ebd313  ISIS-1846 thread-safe (atomic) writes to _Context (singleton-store)
8ebd313 is described below

commit 8ebd31355b1ec18800819f915a41acf166c79d85
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Jan 24 14:17:19 2018 +0100

    ISIS-1846 thread-safe (atomic) writes to _Context (singleton-store)
---
 .../isis/applib/internal/context/_Context.java     | 27 +++++++++++++++++-----
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/core/applib/src/main/java/org/apache/isis/applib/internal/context/_Context.java b/core/applib/src/main/java/org/apache/isis/applib/internal/context/_Context.java
index 5d4889c..adb6d01 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/internal/context/_Context.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/internal/context/_Context.java
@@ -29,7 +29,8 @@ import org.apache.isis.applib.internal.base._Casts;
 /**
  * <h1>- internal use only -</h1>
  * <p>
- * Provides a context for storing and retrieving singletons (usually application scoped).
+ * Provides a context for storing and retrieving singletons (usually application scoped). 
+ * Writes to the store are implemented thread-safe. 
  * </p>
  * <p>
  * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/> 
@@ -41,6 +42,13 @@ public final class _Context {
 
 	private _Context(){}
 	
+	/**
+	 * Thread-safety note: We let threads synchronize on writes to the singletonMap, 
+	 * NOT on reads.<br/>
+	 * If there is a race-condition between a writing and a reading thread, by design 
+	 * the first one wins.<br/> 
+	 * If synchronization is required it should happen elsewhere, not here!<br/>
+	 */
 	private final static Map<String, Object> singletonMap = new HashMap<>(); 
 
 	/**
@@ -54,10 +62,13 @@ public final class _Context {
 		Objects.requireNonNull(type);
 		Objects.requireNonNull(singleton);
 		
-		if(singletonMap.containsKey(toKey(type)))
-			throw new IllegalStateException("there is already a singleton of type '"+type+"' on this context.");
-		
-		singletonMap.put(toKey(type), singleton);
+		// let writes to the map be atomic
+		synchronized (singletonMap) {   
+			if(singletonMap.containsKey(toKey(type)))
+				throw new IllegalStateException(
+						"there is already a singleton of type '"+type+"' on this context.");
+			singletonMap.put(toKey(type), singleton);	
+		}
 	}
 
 	/**
@@ -110,7 +121,11 @@ public final class _Context {
 	 * Removes any singleton references from the current context.
 	 */
 	public static void clear() {
-		singletonMap.clear();
+		
+		// let writes to the map be atomic
+		synchronized (singletonMap) {
+			singletonMap.clear();
+		}
 	}
 	
 	// -- DEFAULT CLASSLOADER

-- 
To stop receiving notification emails like this one, please contact
ahuber@apache.org.