-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support OpenSlide cache management API
Use the same semantics as the OpenSlide API. The OpenSlide class implements java.io.Closeable, which is documented to be for "a source or destination of data that can be closed". OpenSlideCache is technically a source or destination of data, but not at the Java API level, and the only reason it needs to be closeable is to release the underlying resource in the C API. Its superinterface java.lang.AutoCloseable is documented to be for "an object that may hold resources [...] until it is closed"; implement that instead. OpenSlide has a legacy dispose() method which is an alias for close(). Don't add a similar method to OpenSlideCache. Closes: #36 Signed-off-by: Benjamin Gilbert <[email protected]>
- Loading branch information
Showing
5 changed files
with
124 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* OpenSlide, a library for reading whole slide image files | ||
* | ||
* Copyright (c) 2007-2011 Carnegie Mellon University | ||
* Copyright (c) 2024 Benjamin Gilbert | ||
* All rights reserved. | ||
* | ||
* OpenSlide is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, version 2.1. | ||
* | ||
* OpenSlide is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with OpenSlide. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package org.openslide; | ||
|
||
import java.lang.foreign.MemorySegment; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public final class OpenSlideCache implements AutoCloseable { | ||
private final Lock lock = new ReentrantLock(); | ||
|
||
private MemorySegment cache; | ||
|
||
public OpenSlideCache(long capacity) { | ||
cache = OpenSlideFFM.openslide_cache_create(capacity); | ||
} | ||
|
||
Lock getLock() { | ||
return lock; | ||
} | ||
|
||
// call, and use result, with lock held | ||
MemorySegment getSegment() { | ||
if (cache == null) { | ||
throw new OpenSlideDisposedException("OpenSlideCache"); | ||
} | ||
return cache; | ||
} | ||
|
||
// takes the lock | ||
@Override | ||
public void close() { | ||
lock.lock(); | ||
try { | ||
if (cache != null) { | ||
OpenSlideFFM.openslide_cache_release(cache); | ||
cache = null; | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} |
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