1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
#include <glib-object.h>
27
#include "base-types.h"
30
#include "tile-cache.h"
31
#include "tile-swap.h"
32
#include "tile-private.h"
35
/* This is the percentage of the maximum cache size that should be cleared
36
* from the cache when an eviction is necessary
38
#define FREE_QUANTUM 0.1
40
#define IDLE_SWAPPER_TIMEOUT 250
43
static gboolean tile_cache_zorch_next (void);
44
static void tile_cache_flush_internal (Tile *tile);
47
static gpointer tile_idle_thread (gpointer data);
49
static gboolean tile_idle_preswap (gpointer data);
53
static gboolean initialize = TRUE;
55
typedef struct _TileList
61
static gulong max_tile_size = TILE_WIDTH * TILE_HEIGHT * 4;
62
static gulong cur_cache_size = 0;
63
static gulong max_cache_size = 0;
64
static gulong cur_cache_dirty = 0;
65
static TileList clean_list = { NULL, NULL };
66
static TileList dirty_list = { NULL, NULL };
69
static pthread_t preswap_thread;
70
static pthread_mutex_t dirty_mutex = PTHREAD_MUTEX_INITIALIZER;
71
static pthread_cond_t dirty_signal = PTHREAD_COND_INITIALIZER;
72
static pthread_mutex_t tile_mutex = PTHREAD_MUTEX_INITIALIZER;
73
#define CACHE_LOCK pthread_mutex_lock (&tile_mutex)
74
#define CACHE_UNLOCK pthread_mutex_unlock (&tile_mutex)
76
static guint idle_swapper = 0;
77
#define CACHE_LOCK /* nothing */
78
#define CACHE_UNLOCK /* nothing */
83
tile_cache_init (gulong tile_cache_size)
89
clean_list.first = clean_list.last = NULL;
90
dirty_list.first = dirty_list.last = NULL;
92
max_cache_size = tile_cache_size;
95
pthread_create (&preswap_thread, NULL, &tile_idle_thread, NULL);
97
idle_swapper = g_timeout_add (IDLE_SWAPPER_TIMEOUT,
105
tile_cache_exit (void)
107
tile_cache_set_size (0);
111
tile_cache_insert (Tile *tile)
121
/* First check and see if the tile is already
122
* in the cache. In that case we will simply place
123
* it at the end of the tile list to indicate that
124
* it was the most recently accessed tile.
127
list = (TileList *) tile->listhead;
129
newlist = ((tile->dirty || tile->swap_offset == -1) ?
130
&dirty_list : &clean_list);
132
/* if list is NULL, the tile is not in the cache */
136
/* Tile is in the cache. Remove it from its current list and
137
put it at the tail of the proper list (clean or dirty) */
140
tile->next->prev = tile->prev;
142
list->last = tile->prev;
145
tile->prev->next = tile->next;
147
list->first = tile->next;
149
tile->listhead = NULL;
151
if (list == &dirty_list)
152
cur_cache_dirty -= tile_size_inline (tile);
156
/* The tile was not in the cache. First check and see
157
* if there is room in the cache. If not then we'll have
158
* to make room first. Note: it might be the case that the
159
* cache is smaller than the size of a tile in which case
160
* it won't be possible to put it in the cache.
162
while ((cur_cache_size + max_tile_size) > max_cache_size)
164
if (! tile_cache_zorch_next ())
166
g_warning ("cache: unable to find room for a tile");
171
cur_cache_size += tile_size_inline (tile);
174
/* Put the tile at the end of the proper list */
177
tile->prev = newlist->last;
178
tile->listhead = newlist;
181
newlist->last->next = tile;
183
newlist->first = tile;
185
newlist->last = tile;
187
/* gosgood@idt.net 1999-12-04 */
188
/* bytes on cur_cache_dirty miscounted in CVS 1.12: */
189
/* Invariant: test for selecting dirty list should be the same */
190
/* as counting files dirty. */
192
if (tile->dirty || (tile->swap_offset == -1))
194
cur_cache_dirty += tile_size_inline (tile);
197
pthread_mutex_lock (&dirty_mutex);
198
pthread_cond_signal (&dirty_signal);
199
pthread_mutex_unlock (&dirty_mutex);
208
tile_cache_flush (Tile *tile)
212
tile_cache_flush_internal (tile);
218
tile_cache_flush_internal (Tile *tile)
222
/* Find where the tile is in the cache.
225
list = (TileList *) tile->listhead;
229
cur_cache_size -= tile_size_inline (tile);
231
if (list == &dirty_list)
232
cur_cache_dirty -= tile_size_inline (tile);
235
tile->next->prev = tile->prev;
237
list->last = tile->prev;
240
tile->prev->next = tile->next;
242
list->first = tile->next;
244
tile->listhead = NULL;
250
tile_cache_set_size (gulong cache_size)
254
max_cache_size = cache_size;
256
while (cur_cache_size > max_cache_size)
258
if (!tile_cache_zorch_next ())
267
tile_cache_zorch_next (void)
271
if (clean_list.first)
272
tile = clean_list.first;
273
else if (dirty_list.first)
274
tile = dirty_list.first;
279
TILE_MUTEX_LOCK (tile);
282
tile_cache_flush_internal (tile);
284
if (tile->dirty || tile->swap_offset == -1)
286
tile_swap_out (tile);
293
TILE_MUTEX_UNLOCK (tile);
298
/* unable to swap out tile for some reason */
299
TILE_MUTEX_UNLOCK (tile);
308
tile_idle_thread (gpointer data)
314
g_printerr ("starting tile preswapper thread\n");
321
if (count > 5 || dirty_list.first == NULL)
327
pthread_mutex_lock (&dirty_mutex);
328
pthread_cond_wait (&dirty_signal, &dirty_mutex);
329
pthread_mutex_unlock (&dirty_mutex);
334
if ((tile = dirty_list.first))
337
TILE_MUTEX_LOCK (tile);
340
if (tile->dirty || tile->swap_offset == -1)
342
list = tile->listhead;
344
if (list == &dirty_list)
345
cur_cache_dirty -= tile_size_inline (tile);
348
tile->next->prev = tile->prev;
350
list->last = tile->prev;
353
tile->prev->next = tile->next;
355
list->first = tile->next;
358
tile->prev = clean_list.last;
359
tile->listhead = &clean_list;
362
clean_list.last->next = tile;
364
clean_list.first = tile;
366
clean_list.last = tile;
370
tile_swap_out (tile);
377
TILE_MUTEX_UNLOCK (tile);
388
#else /* !USE_PTHREADS */
391
tile_idle_preswap (gpointer data)
395
if (cur_cache_dirty * 2 < max_cache_size)
398
if ((tile = dirty_list.first))
400
tile_swap_out (tile);
402
dirty_list.first = tile->next;
405
tile->next->prev = NULL;
407
dirty_list.last = NULL;
410
tile->prev = clean_list.last;
411
tile->listhead = &clean_list;
414
clean_list.last->next = tile;
416
clean_list.first = tile;
418
clean_list.last = tile;
419
cur_cache_dirty -= tile_size_inline (tile);
425
#endif /* !USE_PTHREADS */