Gimp/app/base/tile-cache.c
Sven Neumann 073e533a8a Finally landed the new GimpConfig based gimprc parser. It's not finished
2002-11-18  Sven Neumann  <sven@gimp.org>

	Finally landed the new GimpConfig based gimprc parser. It's not
	finished yet but we need to start somewhere. This release removes
	the old gimprc.[ch] files. The gimprc format changes slightly, but
	the changes are minimal. The Preferences dialog is temporarily
	disabled since it still needs to be ported. If you are are afraid,
	stay away from CVS for a few days ;-)

	* app/Makefile.am
	* app/gimprc.[ch]: removed the old gimprc system.

	* app/base/Makefile.am
	* app/base/base-config.[ch]: removed these files in favor of
	config/gimpbaseconfig.[ch].

	* app/core/Makefile.am
	* app/core/gimpcoreconfig.[ch]: removed these files in favor of
	config/gimpcoreconfig.[ch].

	* app/config/Makefile.am
	* app/config/config-types.h: moved typedefs into this new file.

	* app/config/gimpbaseconfig.[ch]
	* app/config/gimpcoreconfig.[ch]
	* app/config/gimpdisplayconfig.[ch]
	* app/config/gimpguiconfig.[ch]
	* app/config/gimprc.[ch]
	* app/config/test-config.c: brought into shape for real use.

	* app/base/base-types.h: include config/config-types.h here. Added
	a global GimpBaseConfig *base_config variable to ease migration.

	* app/gui/Makefile.am: temporarily disabled the preferences dialog.

	* app/app_procs.c
	* app/undo.c
	* app/undo_history.c
	* app/base/base.[ch]
	* app/base/gimphistogram.c
	* app/base/pixel-processor.c
	* app/base/temp-buf.c
	* app/base/tile-cache.c
	* app/core/core-types.h
	* app/core/gimp-documents.c
	* app/core/gimp.c
	* app/core/gimpbrush.c
	* app/core/gimpbrushgenerated.c
	* app/core/gimpcontext.c
	* app/core/gimpdrawable-transform.c
	* app/core/gimpimage-new.c
	* app/core/gimpimage.c
	* app/core/gimpimagefile.c
	* app/core/gimpmodules.c
	* app/core/gimppattern.c
	* app/display/Makefile.am
	* app/display/gimpdisplay-handlers.c
	* app/display/gimpdisplay.[ch]
	* app/display/gimpdisplayshell-callbacks.c
	* app/display/gimpdisplayshell-handlers.c
	* app/display/gimpdisplayshell-layer-select.c
	* app/display/gimpdisplayshell-render.c
	* app/display/gimpdisplayshell-scale.c
	* app/display/gimpdisplayshell-scroll.c
	* app/display/gimpdisplayshell-selection.c
	* app/display/gimpdisplayshell.[ch]
	* app/display/gimpnavigationview.c
	* app/file/file-save.c
	* app/gui/device-status-dialog.c
	* app/gui/dialogs-constructors.c
	* app/gui/file-commands.c
	* app/gui/file-new-dialog.c
	* app/gui/file-open-dialog.c
	* app/gui/file-save-dialog.c
	* app/gui/gui.c
	* app/gui/menus.c
	* app/gui/paths-dialog.c
	* app/gui/resize-dialog.c
	* app/gui/session.c
	* app/gui/test-commands.c
	* app/gui/tips-dialog.c
	* app/gui/tips-dialog.h
	* app/gui/user-install-dialog.c
	* app/gui/view-commands.c
	* app/paint/gimppaintcore.c
	* app/plug-in/plug-in.c
	* app/plug-in/plug-ins.c
	* app/tools/gimpbezierselecttool.c
	* app/tools/gimpbucketfilltool.c
	* app/tools/gimpcolorpickertool.c
	* app/tools/gimpcroptool.c
	* app/tools/gimpeditselectiontool.c
	* app/tools/gimpfuzzyselecttool.c
	* app/tools/gimpinktool.c
	* app/tools/gimpmagnifytool.c
	* app/tools/gimpmeasuretool.c
	* app/tools/gimppainttool.c
	* app/tools/gimppathtool.c
	* app/tools/gimptexttool.[ch]
	* app/tools/selection_options.c
	* app/tools/tools.c
	* app/tools/transform_options.c
	* app/widgets/gimphelp.c
	* app/widgets/gimpitemfactory.c
	* app/widgets/gimpselectioneditor.c
	* app/xcf/xcf-load.c
	* tools/pdbgen/pdb/fileops.pdb
	* tools/pdbgen/pdb/gimprc.pdb
	* tools/pdbgen/pdb/image.pdb
	* tools/pdbgen/pdb/layer.pdb
	* tools/pdbgen/pdb/transform_tools.pdb: use the new config system
	instead of the old gimprc stuff.

	* etc/gimprc.in
	* etc/gimprc_user.in: adapted to the new gimprc format. Will update
	the man-page later...

	* app/pdb/fileops_cmds.c
	* app/pdb/gimprc_cmds.c
	* app/pdb/image_cmds.c
	* app/pdb/layer_cmds.c
	* app/pdb/transform_tools_cmds.c
	* libgimp/gimpgimprc_pdb.c: regenerated.
2002-11-18 20:50:31 +00:00

427 lines
9 KiB
C

/* 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 "config/gimpbaseconfig.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 void tile_cache_init (void);
static gint 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_insert (Tile *tile)
{
TileList *list;
TileList *newlist;
if (initialize)
tile_cache_init ();
CACHE_LOCK;
if (tile->data == NULL) 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 (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;
/* goto grump;*/
}
}
/*grump:*/
/* Note the increase in the number of bytes the cache
* is referencing.
*/
cur_cache_size += tile_size (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 (tile);
if (1)
{
#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)
{
if (initialize)
tile_cache_init ();
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)
{
/* Note the decrease in the number of bytes the cache
* is referencing.
*/
cur_cache_size -= tile_size (tile);
if (list == &dirty_list)
cur_cache_dirty -= tile_size (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;
}
}
/* untested -- ADM */
void
tile_cache_set_size (gulong cache_size)
{
if (initialize)
tile_cache_init ();
max_cache_size = cache_size;
CACHE_LOCK;
while (cur_cache_size > max_cache_size)
{
if (!tile_cache_zorch_next ())
break;
}
CACHE_UNLOCK;
}
static void
tile_cache_init (void)
{
if (initialize)
{
initialize = FALSE;
clean_list.first = clean_list.last = NULL;
dirty_list.first = dirty_list.last = NULL;
max_cache_size = base_config->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
}
}
static gint
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\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 (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 (tile);
}
return TRUE;
}
#endif