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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#ifdef USE_PTHREADS
#include <pthread.h>
#endif
#include <glib-object.h>
#include "base-types.h"
#include "tile.h"
#include "tile-cache.h"
#include "tile-swap.h"
#include "tile-private.h"
/* This is the percentage of the maximum cache size that should be cleared
* from the cache when an eviction is necessary
*/
#define FREE_QUANTUM 0.1
#define IDLE_SWAPPER_TIMEOUT 250
static gboolean tile_cache_zorch_next (void);
static void tile_cache_flush_internal (Tile *tile);
#ifdef USE_PTHREADS
static gpointer tile_idle_thread (gpointer data);
#else
static gboolean tile_idle_preswap (gpointer data);
#endif
static gboolean initialize = TRUE;
typedef struct _TileList
{
Tile *first;
Tile *last;
} TileList;
static gulong max_tile_size = TILE_WIDTH * TILE_HEIGHT * 4;
static gulong cur_cache_size = 0;
static gulong max_cache_size = 0;
static gulong cur_cache_dirty = 0;
static TileList clean_list = { NULL, NULL };
static TileList dirty_list = { NULL, NULL };
#ifdef USE_PTHREADS
static pthread_t preswap_thread;
static pthread_mutex_t dirty_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t dirty_signal = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t tile_mutex = PTHREAD_MUTEX_INITIALIZER;
#define CACHE_LOCK pthread_mutex_lock (&tile_mutex)
#define CACHE_UNLOCK pthread_mutex_unlock (&tile_mutex)
#else
static guint idle_swapper = 0;
#define CACHE_LOCK /* nothing */
#define CACHE_UNLOCK /* nothing */
#endif
void
tile_cache_init (gulong tile_cache_size)
{
if (initialize)
{
initialize = FALSE;
clean_list.first = clean_list.last = NULL;
dirty_list.first = dirty_list.last = NULL;
max_cache_size = tile_cache_size;
#ifdef USE_PTHREADS
pthread_create (&preswap_thread, NULL, &tile_idle_thread, NULL);
#else
idle_swapper = g_timeout_add (IDLE_SWAPPER_TIMEOUT,
tile_idle_preswap,
NULL);
#endif
}
}
void
tile_cache_exit (void)
{
tile_cache_set_size (0);
}
void
tile_cache_insert (Tile *tile)
{
TileList *list;
TileList *newlist;
CACHE_LOCK;
if (! tile->data)
goto out;
/* First check and see if the tile is already
* in the cache. In that case we will simply place
* it at the end of the tile list to indicate that
* it was the most recently accessed tile.
*/
list = (TileList *) tile->listhead;
newlist = ((tile->dirty || tile->swap_offset == -1) ?
&dirty_list : &clean_list);
/* if list is NULL, the tile is not in the cache */
if (list)
{
/* Tile is in the cache. Remove it from its current list and
put it at the tail of the proper list (clean or dirty) */
if (tile->next)
tile->next->prev = tile->prev;
else
list->last = tile->prev;
if (tile->prev)
tile->prev->next = tile->next;
else
list->first = tile->next;
tile->listhead = NULL;
if (list == &dirty_list)
cur_cache_dirty -= tile_size_inline (tile);
}
else
{
/* The tile was not in the cache. First check and see
* if there is room in the cache. If not then we'll have
* to make room first. Note: it might be the case that the
* cache is smaller than the size of a tile in which case
* it won't be possible to put it in the cache.
*/
while ((cur_cache_size + max_tile_size) > max_cache_size)
{
if (! tile_cache_zorch_next ())
{
g_warning ("cache: unable to find room for a tile");
goto out;
}
}
cur_cache_size += tile_size_inline (tile);
}
/* Put the tile at the end of the proper list */
tile->next = NULL;
tile->prev = newlist->last;
tile->listhead = newlist;
if (newlist->last)
newlist->last->next = tile;
else
newlist->first = tile;
newlist->last = tile;
/* gosgood@idt.net 1999-12-04 */
/* bytes on cur_cache_dirty miscounted in CVS 1.12: */
/* Invariant: test for selecting dirty list should be the same */
/* as counting files dirty. */
if (tile->dirty || (tile->swap_offset == -1))
{
cur_cache_dirty += tile_size_inline (tile);
#ifdef USE_PTHREADS
pthread_mutex_lock (&dirty_mutex);
pthread_cond_signal (&dirty_signal);
pthread_mutex_unlock (&dirty_mutex);
#endif
}
out:
CACHE_UNLOCK;
}
void
tile_cache_flush (Tile *tile)
{
CACHE_LOCK;
tile_cache_flush_internal (tile);
CACHE_UNLOCK;
}
static void
tile_cache_flush_internal (Tile *tile)
{
TileList *list;
/* Find where the tile is in the cache.
*/
list = (TileList *) tile->listhead;
if (list)
{
cur_cache_size -= tile_size_inline (tile);
if (list == &dirty_list)
cur_cache_dirty -= tile_size_inline (tile);
if (tile->next)
tile->next->prev = tile->prev;
else
list->last = tile->prev;
if (tile->prev)
tile->prev->next = tile->next;
else
list->first = tile->next;
tile->listhead = NULL;
}
}
void
tile_cache_set_size (gulong cache_size)
{
CACHE_LOCK;
max_cache_size = cache_size;
while (cur_cache_size > max_cache_size)
{
if (!tile_cache_zorch_next ())
break;
}
CACHE_UNLOCK;
}
static gboolean
tile_cache_zorch_next (void)
{
Tile *tile;
if (clean_list.first)
tile = clean_list.first;
else if (dirty_list.first)
tile = dirty_list.first;
else
return FALSE;
CACHE_UNLOCK;
TILE_MUTEX_LOCK (tile);
CACHE_LOCK;
tile_cache_flush_internal (tile);
if (tile->dirty || tile->swap_offset == -1)
{
tile_swap_out (tile);
}
if (! tile->dirty)
{
g_free (tile->data);
tile->data = NULL;
TILE_MUTEX_UNLOCK (tile);
return TRUE;
}
/* unable to swap out tile for some reason */
TILE_MUTEX_UNLOCK (tile);
return FALSE;
}
#if USE_PTHREADS
static gpointer
tile_idle_thread (gpointer data)
{
Tile *tile;
TileList *list;
gint count;
g_printerr ("starting tile preswapper thread\n");
count = 0;
while (TRUE)
{
CACHE_LOCK;
if (count > 5 || dirty_list.first == NULL)
{
CACHE_UNLOCK;
count = 0;
pthread_mutex_lock (&dirty_mutex);
pthread_cond_wait (&dirty_signal, &dirty_mutex);
pthread_mutex_unlock (&dirty_mutex);
CACHE_LOCK;
}
if ((tile = dirty_list.first))
{
CACHE_UNLOCK;
TILE_MUTEX_LOCK (tile);
CACHE_LOCK;
if (tile->dirty || tile->swap_offset == -1)
{
list = tile->listhead;
if (list == &dirty_list)
cur_cache_dirty -= tile_size_inline (tile);
if (tile->next)
tile->next->prev = tile->prev;
else
list->last = tile->prev;
if (tile->prev)
tile->prev->next = tile->next;
else
list->first = tile->next;
tile->next = NULL;
tile->prev = clean_list.last;
tile->listhead = &clean_list;
if (clean_list.last)
clean_list.last->next = tile;
else
clean_list.first = tile;
clean_list.last = tile;
CACHE_UNLOCK;
tile_swap_out (tile);
}
else
{
CACHE_UNLOCK;
}
TILE_MUTEX_UNLOCK (tile);
}
else
{
CACHE_UNLOCK;
}
count++;
}
}
#else /* !USE_PTHREADS */
static gboolean
tile_idle_preswap (gpointer data)
{
Tile *tile;
if (cur_cache_dirty * 2 < max_cache_size)
return TRUE;
if ((tile = dirty_list.first))
{
tile_swap_out (tile);
dirty_list.first = tile->next;
if (tile->next)
tile->next->prev = NULL;
else
dirty_list.last = NULL;
tile->next = NULL;
tile->prev = clean_list.last;
tile->listhead = &clean_list;
if (clean_list.last)
clean_list.last->next = tile;
else
clean_list.first = tile;
clean_list.last = tile;
cur_cache_dirty -= tile_size_inline (tile);
}
return TRUE;
}
#endif /* !USE_PTHREADS */
|