1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/* Share and reuse openslide_t between many openslideload operations.
*
* 1/4/25
* - first version!
*/
/*
This file is part of VIPS.
VIPS 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; either version 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
#define VIPS_DEBUG
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#ifdef HAVE_OPENSLIDE
#include <vips/vips.h>
#include <vips/internal.h>
#include "pforeign.h"
#include <openslide.h>
// we keep this many connections around after they are closed, ready for reuse
#define OPENSLIDECONNECTION (3)
// a tile cache shared between all active openslide connections ... 32mb
#define OPENSLIDECONNECTION_CACHE_SIZE (64 * 1024 * 1024)
typedef struct _VipsOpenslideConnection {
char *filename;
// protected by vips_openslideconnection_lock
int ref_count;
// first access protected by separate lock, since initialization
// is slow
openslide_t *osr;
GMutex osr_lock;
} VipsOpenslideConnection;
static GHashTable *vips_openslideconnection_cache = NULL;
static GQueue *vips_openslideconnection_unused = NULL;
static GMutex vips_openslideconnection_lock;
/* Added in 4.0 ... this is a tile cache that's shared between all active
* openslide connections.
*/
#ifdef HAVE_OPENSLIDE_CACHE_CREATE
openslide_cache_t *vips_openslideconnection_openslide_cache;
#endif /*HAVE_OPENSLIDE_CACHE_CREATE*/
static void
vips_openslideconnection_free(VipsOpenslideConnection *connection)
{
#ifdef DEBUG
printf("vips_openslideconnection_free: %s\n", connection->filename);
#endif /*DEBUG*/
VipsOpenslideConnection *cached G_GNUC_UNUSED =
g_hash_table_lookup(vips_openslideconnection_cache,
connection->filename);
g_assert(cached);
g_assert(cached == connection);
g_hash_table_remove(vips_openslideconnection_cache, connection->filename);
g_queue_remove(vips_openslideconnection_unused, connection);
g_mutex_lock(&connection->osr_lock);
VIPS_FREEF(openslide_close, connection->osr);
g_mutex_unlock(&connection->osr_lock);
g_mutex_clear(&connection->osr_lock);
VIPS_FREE(connection->filename);
g_free(connection);
}
static void
vips_openslideconnection_trim(void)
{
while (vips_openslideconnection_unused->length > OPENSLIDECONNECTION) {
VipsOpenslideConnection *oldest =
g_queue_pop_head(vips_openslideconnection_unused);
vips_openslideconnection_free(oldest);
}
}
static void
vips_openslideconnection_unref(VipsOpenslideConnection *connection)
{
#ifdef DEBUG
printf("vips_openslideconnection_unref: %s\n", connection->filename);
#endif /*DEBUG*/
g_assert(connection->ref_count > 0);
connection->ref_count -= 1;
if (connection->ref_count == 0) {
/* If the openslide_t is in an error state, or we've no connection,
* don't leave it in the cache.
*/
if (!connection->osr ||
openslide_get_error(connection->osr))
vips_openslideconnection_free(connection);
else {
g_queue_push_tail(vips_openslideconnection_unused, connection);
vips_openslideconnection_trim();
}
}
}
static void
vips_openslideconnection_ref(VipsOpenslideConnection *connection)
{
#ifdef DEBUG
printf("vips_openslideconnection_ref: %s\n", connection->filename);
#endif /*DEBUG*/
g_assert(connection->ref_count >= 0);
if (connection->ref_count == 0)
g_queue_remove(vips_openslideconnection_unused, connection);
connection->ref_count += 1;
}
static VipsOpenslideConnection *
vips_openslideconnection_new(const char *filename)
{
#ifdef DEBUG
printf("vips_openslideconnection_new: %s\n", filename);
#endif /*DEBUG*/
VipsOpenslideConnection *connection;
connection = g_new0(VipsOpenslideConnection, 1);
connection->filename = g_strdup(filename);
g_mutex_init(&connection->osr_lock);
g_assert(!g_hash_table_lookup(vips_openslideconnection_cache, filename));
g_hash_table_insert(vips_openslideconnection_cache,
connection->filename, connection);
return connection;
}
openslide_t *
vips__openslideconnection_open(const char *filename, gboolean revalidate)
{
#ifdef DEBUG
printf("vips_openslideconnection_open: %s, revalidate = %d\n",
filename, revalidate);
#endif /*DEBUG*/
g_mutex_lock(&vips_openslideconnection_lock);
if (!vips_openslideconnection_cache) {
vips_openslideconnection_cache =
g_hash_table_new(g_str_hash, g_str_equal);
vips_openslideconnection_unused = g_queue_new();
#ifdef HAVE_OPENSLIDE_CACHE_CREATE
vips_openslideconnection_openslide_cache =
openslide_cache_create(OPENSLIDECONNECTION_CACHE_SIZE);
#endif /*HAVE_OPENSLIDE_CACHE_CREATE*/
}
VipsOpenslideConnection *connection;
connection = g_hash_table_lookup(vips_openslideconnection_cache, filename);
// discard any cached connection on revalidate
if (connection &&
connection->ref_count == 0 &&
revalidate)
VIPS_FREEF(vips_openslideconnection_free, connection);
if (!connection)
connection = vips_openslideconnection_new(filename);
vips_openslideconnection_ref(connection);
g_mutex_unlock(&vips_openslideconnection_lock);
g_mutex_lock(&connection->osr_lock);
gboolean unref;
/* We do the open outside the main lock (just in the connection lock)
* since it can take many seconds for some slides.
*/
unref = FALSE;
if (!connection->osr) {
connection->osr = openslide_open(connection->filename);
/* If open fails, we must unref the connection, since we'll return
* NULL.
*/
if (!connection->osr)
unref = TRUE;
#ifdef HAVE_OPENSLIDE_CACHE_CREATE
if (connection->osr)
openslide_set_cache(connection->osr,
vips_openslideconnection_openslide_cache);
#endif /*HAVE_OPENSLIDE_CACHE_CREATE*/
}
openslide_t *osr = connection->osr;
g_mutex_unlock(&connection->osr_lock);
if (unref) {
g_mutex_lock(&vips_openslideconnection_lock);
vips_openslideconnection_unref(connection);
g_mutex_unlock(&vips_openslideconnection_lock);
}
return osr;
}
void
vips__openslideconnection_close(const char *filename)
{
#ifdef DEBUG
printf("vips_openslideconnection_close: %s\n", filename);
#endif /*DEBUG*/
g_mutex_lock(&vips_openslideconnection_lock);
VipsOpenslideConnection *connection;
connection = g_hash_table_lookup(vips_openslideconnection_cache, filename);
if (connection)
vips_openslideconnection_unref(connection);
g_mutex_unlock(&vips_openslideconnection_lock);
}
int
vips__openslideconnection_leak(void)
{
int n_leaks;
n_leaks = 0;
g_mutex_lock(&vips_openslideconnection_lock);
if (vips_openslideconnection_cache)
n_leaks += g_hash_table_size(vips_openslideconnection_cache) -
vips_openslideconnection_unused->length;
g_mutex_unlock(&vips_openslideconnection_lock);
if (n_leaks > 0)
printf("vips__openslideconnection_leak: %d leaked connections\n",
n_leaks);
return n_leaks;
}
#endif /*HAVE_OPENSLIDE*/
|