-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add InstanceScopedThreadSafeAccess
- Loading branch information
1 parent
0ee0c35
commit 0d39682
Showing
6 changed files
with
93 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ain/java/org/eclipse/digitaltwin/basyx/common/backend/InstanceScopedThreadSafeAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.eclipse.digitaltwin.basyx.common.backend; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
|
||
public class InstanceScopedThreadSafeAccess { | ||
private final ConcurrentHashMap<Object, ThreadSafeAccess> accessMap = new ConcurrentHashMap<>(); | ||
|
||
public <T> T read(Supplier<T> supplier, Object instanceLock) { | ||
return getAccess(instanceLock).read(supplier); | ||
} | ||
|
||
public void read(Runnable action, Object instanceLock) { | ||
getAccess(instanceLock).read(action); | ||
} | ||
|
||
public <T> T write(Supplier<T> supplier, Object instanceLock) { | ||
return getAccess(instanceLock).write(supplier); | ||
} | ||
|
||
public void write(Runnable action, Object instanceLock) { | ||
getAccess(instanceLock).write(action); | ||
} | ||
|
||
public void removeLock(Object instanceLock) { | ||
accessMap.remove(instanceLock); | ||
} | ||
|
||
private ThreadSafeAccess getAccess(Object instanceLock) { | ||
return accessMap.computeIfAbsent(instanceLock, k -> new ThreadSafeAccess()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.