Author: abrander
Date: 2010-01-14 19:11:23 +0100 (Thu, 14 Jan 2010)
New Revision: 2989
Added:
branches/rawstudio-ng-color/librawstudio/rs-io-job-checksum.c
branches/rawstudio-ng-color/librawstudio/rs-io-job-checksum.h
branches/rawstudio-ng-color/librawstudio/rs-io-job-metadata.c
branches/rawstudio-ng-color/librawstudio/rs-io-job-metadata.h
branches/rawstudio-ng-color/librawstudio/rs-io-job-prefetch.c
branches/rawstudio-ng-color/librawstudio/rs-io-job-prefetch.h
branches/rawstudio-ng-color/librawstudio/rs-io-job.c
branches/rawstudio-ng-color/librawstudio/rs-io-job.h
branches/rawstudio-ng-color/librawstudio/rs-io.c
branches/rawstudio-ng-color/librawstudio/rs-io.h
Modified:
branches/rawstudio-ng-color/librawstudio/Makefile.am
branches/rawstudio-ng-color/librawstudio/rawstudio.h
Log:
Added simple i/o abstraction.
Modified: branches/rawstudio-ng-color/librawstudio/Makefile.am
===================================================================
--- branches/rawstudio-ng-color/librawstudio/Makefile.am 2010-01-14
13:16:25 UTC (rev 2988)
+++ branches/rawstudio-ng-color/librawstudio/Makefile.am 2010-01-14
18:11:23 UTC (rev 2989)
@@ -9,6 +9,11 @@
library_includedir=$(includedir)/@pack...@-@VERSION@/
library_include_HEADERS = rawstudio.h rs-types.h rs-macros.h \
+ rs-io-job.h \
+ rs-io-job-checksum.h \
+ rs-io-job-metadata.h \
+ rs-io-job-prefetch.h \
+ rs-io.h \
rs-plugin.h \
rs-rawfile.h \
rs-exif.h \
@@ -50,6 +55,11 @@
lib_LTLIBRARIES = librawstudio-1.1.la
librawstudio_1_1_la_SOURCES = \
+ rs-io-job.c rs-io-job.h \
+ rs-io-job-checksum.c rs-io-job-checksum.h \
+ rs-io-job-metadata.c rs-io-job-metadata.h \
+ rs-io-job-prefetch.c rs-io-job-prefetch.h \
+ rs-io.c rs-io.h \
rs-plugin.c rs-plugin.h \
rs-rawfile.c rs-rawfile.h \
rs-exif.cc rs-exif.h \
Modified: branches/rawstudio-ng-color/librawstudio/rawstudio.h
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rawstudio.h 2010-01-14
13:16:25 UTC (rev 2988)
+++ branches/rawstudio-ng-color/librawstudio/rawstudio.h 2010-01-14
18:11:23 UTC (rev 2989)
@@ -29,6 +29,11 @@
#include "rs-macros.h"
+#include "rs-io-job.h"
+#include "rs-io-job-checksum.h"
+#include "rs-io-job-metadata.h"
+#include "rs-io-job-prefetch.h"
+#include "rs-io.h"
#include "rs-rawfile.h"
#include "rs-settings.h"
#include "rs-exif.h"
Added: branches/rawstudio-ng-color/librawstudio/rs-io-job-checksum.c
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rs-io-job-checksum.c
(rev 0)
+++ branches/rawstudio-ng-color/librawstudio/rs-io-job-checksum.c
2010-01-14 18:11:23 UTC (rev 2989)
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <[email protected]> and
+ * Anders Kvist <[email protected]>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
+ */
+
+#include "rs-io-job-checksum.h"
+#include "rawstudio.h"
+
+typedef struct {
+ RSIoJob parent;
+ gboolean dispose_has_run;
+
+ gchar *path;
+ gchar *checksum;
+ RSGotChecksumCB callback;
+} RSIoJobChecksum;
+
+G_DEFINE_TYPE(RSIoJobChecksum, rs_io_job_checksum, RS_TYPE_IO_JOB)
+
+static void
+execute(RSIoJob *job)
+{
+ RSIoJobChecksum *checksum = RS_IO_JOB_CHECKSUM(job);
+
+ rs_io_lock();
+ checksum->checksum = rs_file_checksum(checksum->path);
+ rs_io_unlock();
+}
+
+static void
+do_callback(RSIoJob *job)
+{
+ RSIoJobChecksum *checksum = RS_IO_JOB_CHECKSUM(job);
+
+ if (checksum->callback && checksum->checksum)
+ checksum->callback(checksum->checksum, job->user_data);
+}
+
+static void
+rs_io_job_checksum_dispose(GObject *object)
+{
+ RSIoJobChecksum *checksum = RS_IO_JOB_CHECKSUM(object);
+ if (!checksum->dispose_has_run)
+ {
+ checksum->dispose_has_run = TRUE;
+
+ g_free(checksum->path);
+ g_free(checksum->checksum);
+ }
+ G_OBJECT_CLASS (rs_io_job_checksum_parent_class)->dispose(object);
+}
+
+static void
+rs_io_job_checksum_class_init(RSIoJobChecksumClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS(klass);
+ RSIoJobClass *job_class = RS_IO_JOB_CLASS(klass);
+
+ object_class->dispose = rs_io_job_checksum_dispose;
+ job_class->execute = execute;
+ job_class->do_callback = do_callback;
+}
+
+static void
+rs_io_job_checksum_init(RSIoJobChecksum *checksum)
+{
+}
+
+RSIoJob *
+rs_io_job_checksum_new(const gchar *path, RSGotChecksumCB callback)
+{
+ RSIoJobChecksum *checksum = g_object_new(RS_TYPE_IO_JOB_CHECKSUM, NULL);
+
+ checksum->path = g_strdup(path);
+ checksum->callback = callback;
+
+ return RS_IO_JOB(checksum);
+}
Added: branches/rawstudio-ng-color/librawstudio/rs-io-job-checksum.h
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rs-io-job-checksum.h
(rev 0)
+++ branches/rawstudio-ng-color/librawstudio/rs-io-job-checksum.h
2010-01-14 18:11:23 UTC (rev 2989)
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <[email protected]> and
+ * Anders Kvist <[email protected]>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
+ */
+
+#ifndef RS_IO_JOB_CHECKSUM_H
+#define RS_IO_JOB_CHECKSUM_H
+
+#include "rs-io-job.h"
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define RS_TYPE_IO_JOB_CHECKSUM rs_io_job_checksum_get_type()
+#define RS_IO_JOB_CHECKSUM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),
RS_TYPE_IO_JOB_CHECKSUM, RSIoJobChecksum))
+#define RS_IO_JOB_CHECKSUM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass),
RS_TYPE_IO_JOB_CHECKSUM, RSIoJobChecksumClass))
+#define RS_IS_IO_JOB_CHECKSUM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj),
RS_TYPE_IO_JOB_CHECKSUM))
+#define RS_IS_IO_JOB_CHECKSUM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),
RS_TYPE_IO_JOB_CHECKSUM))
+#define RS_IO_JOB_CHECKSUM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),
RS_TYPE_IO_JOB_CHECKSUM, RSIoJobChecksumClass))
+
+typedef void (*RSGotChecksumCB)(const gchar *checksum, gpointer user_data);
+
+typedef struct {
+ RSIoJobClass parent_class;
+} RSIoJobChecksumClass;
+
+GType rs_io_job_checksum_get_type(void);
+
+RSIoJob *rs_io_job_checksum_new(const gchar *path, RSGotChecksumCB callback);
+
+G_END_DECLS
+
+#endif /* RS_IO_JOB_CHECKSUM_H */
Added: branches/rawstudio-ng-color/librawstudio/rs-io-job-metadata.c
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rs-io-job-metadata.c
(rev 0)
+++ branches/rawstudio-ng-color/librawstudio/rs-io-job-metadata.c
2010-01-14 18:11:23 UTC (rev 2989)
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <[email protected]> and
+ * Anders Kvist <[email protected]>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
+ */
+
+#include "rs-io-job-metadata.h"
+#include "rawstudio.h"
+
+typedef struct {
+ RSIoJob parent;
+ gboolean dispose_has_run;
+
+ gchar *path;
+ RSMetadata *metadata;
+ RSGotMetadataCB callback;
+} RSIoJobMetadata;
+
+G_DEFINE_TYPE(RSIoJobMetadata, rs_io_job_metadata, RS_TYPE_IO_JOB)
+
+static void
+execute(RSIoJob *job)
+{
+ RSIoJobMetadata *metadata = RS_IO_JOB_METADATA(job);
+
+ rs_io_lock();
+ metadata->metadata = rs_metadata_new_from_file(metadata->path);
+ rs_io_unlock();
+}
+
+static void
+do_callback(RSIoJob *job)
+{
+ RSIoJobMetadata *metadata = RS_IO_JOB_METADATA(job);
+
+ if (metadata->callback && metadata->metadata)
+ metadata->callback(metadata->metadata, job->user_data);
+}
+
+static void
+rs_io_job_metadata_dispose(GObject *object)
+{
+ RSIoJobMetadata *metadata = RS_IO_JOB_METADATA(object);
+ if (!metadata->dispose_has_run)
+ {
+ metadata->dispose_has_run = TRUE;
+
+ g_free(metadata->path);
+ g_object_unref(metadata->metadata);
+ }
+ G_OBJECT_CLASS(rs_io_job_metadata_parent_class)->dispose(object);
+}
+
+static void
+rs_io_job_metadata_class_init(RSIoJobMetadataClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS(klass);
+ RSIoJobClass *job_class = RS_IO_JOB_CLASS(klass);
+
+ object_class->dispose = rs_io_job_metadata_dispose;
+ job_class->execute = execute;
+ job_class->do_callback = do_callback;
+}
+
+static void
+rs_io_job_metadata_init(RSIoJobMetadata *metadata)
+{
+}
+
+RSIoJob *
+rs_io_job_metadata_new(const gchar *path, RSGotMetadataCB callback)
+{
+ RSIoJobMetadata *metadata = g_object_new (RS_TYPE_IO_JOB_METADATA,
NULL);
+
+ metadata->path = g_strdup(path);
+ metadata->callback = callback;
+
+ return RS_IO_JOB(metadata);
+}
Added: branches/rawstudio-ng-color/librawstudio/rs-io-job-metadata.h
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rs-io-job-metadata.h
(rev 0)
+++ branches/rawstudio-ng-color/librawstudio/rs-io-job-metadata.h
2010-01-14 18:11:23 UTC (rev 2989)
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <[email protected]> and
+ * Anders Kvist <[email protected]>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
+ */
+
+#ifndef RS_IO_JOB_METADATA_H
+#define RS_IO_JOB_METADATA_H
+
+#include "rs-types.h"
+#include "rs-io-job.h"
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define RS_TYPE_IO_JOB_METADATA rs_io_job_metadata_get_type()
+#define RS_IO_JOB_METADATA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),
RS_TYPE_IO_JOB_METADATA, RSIoJobMetadata))
+#define RS_IO_JOB_METADATA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass),
RS_TYPE_IO_JOB_METADATA, RSIoJobMetadataClass))
+#define RS_IS_IO_JOB_METADATA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj),
RS_TYPE_IO_JOB_METADATA))
+#define RS_IS_IO_JOB_METADATA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),
RS_TYPE_IO_JOB_METADATA))
+#define RS_IO_JOB_METADATA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),
RS_TYPE_IO_JOB_METADATA, RSIoJobMetadataClass))
+
+typedef void (*RSGotMetadataCB)(RSMetadata *metadata, gpointer user_data);
+
+typedef struct {
+ RSIoJobClass parent_class;
+} RSIoJobMetadataClass;
+
+GType rs_io_job_metadata_get_type(void);
+
+RSIoJob *rs_io_job_metadata_new(const gchar *path, RSGotMetadataCB callback);
+
+G_END_DECLS
+
+#endif /* RS_IO_JOB_METADATA_H */
Added: branches/rawstudio-ng-color/librawstudio/rs-io-job-prefetch.c
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rs-io-job-prefetch.c
(rev 0)
+++ branches/rawstudio-ng-color/librawstudio/rs-io-job-prefetch.c
2010-01-14 18:11:23 UTC (rev 2989)
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <[email protected]> and
+ * Anders Kvist <[email protected]>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
+ */
+
+/* readahead() on Linux */
+#if __gnu_linux__
+#define _GNU_SOURCE
+#endif /* __gnu_linux__ */
+
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include "rs-io-job-prefetch.h"
+
+typedef struct {
+ RSIoJob parent;
+ gboolean dispose_has_run;
+
+ gchar *path;
+} RSIoJobPrefetch;
+
+G_DEFINE_TYPE(RSIoJobPrefetch, rs_io_job_prefetch, RS_TYPE_IO_JOB)
+
+static void
+execute(RSIoJob *job)
+{
+ gint fd;
+ struct stat st;
+ RSIoJobPrefetch *prefetch = RS_IO_JOB_PREFETCH(job);
+
+ stat(prefetch->path, &st);
+ if (st.st_size > 0)
+ {
+ fd = open(prefetch->path, O_RDONLY);
+ if (fd > 0)
+ {
+ gint bytes_read = 0;
+#if __gnu_linux__
+ while(bytes_read < st.st_size)
+ {
+ rs_io_lock();
+ gint length = MIN(st.st_size-bytes_read,
1024*1024);
+ readahead(fd, bytes_read, length);
+ bytes_read += length;
+ rs_io_unlock();
+ }
+#else
+ gchar *tmp = g_new(gchar, st.st_size);
+
+ while(bytes_read < st.st_size)
+ {
+ rs_io_lock();
+ bytes_read += read(fd, tmp+bytes_read,
MIN(st.st_size-bytes_read, 1024*1024));
+ rs_io_unlock();
+ }
+
+ g_free(tmp);
+#endif /* __gnu_linux__ */
+ }
+ }
+}
+
+static void
+rs_io_job_prefetch_dispose(GObject *object)
+{
+ RSIoJobPrefetch *prefetch = RS_IO_JOB_PREFETCH(object);
+ if (!prefetch->dispose_has_run)
+ {
+ prefetch->dispose_has_run = TRUE;
+
+ g_free(prefetch->path);
+ }
+ G_OBJECT_CLASS(rs_io_job_prefetch_parent_class)->dispose(object);
+}
+
+static void
+rs_io_job_prefetch_class_init(RSIoJobPrefetchClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS(klass);
+ RSIoJobClass *job_class = RS_IO_JOB_CLASS(klass);
+
+ object_class->dispose = rs_io_job_prefetch_dispose;
+ job_class->execute = execute;
+}
+
+static void
+rs_io_job_prefetch_init(RSIoJobPrefetch *prefetch)
+{
+}
+
+RSIoJob *
+rs_io_job_prefetch_new(const gchar *path)
+{
+ RSIoJobPrefetch *prefetch = g_object_new(RS_TYPE_IO_JOB_PREFETCH, NULL);
+
+ prefetch->path = g_strdup(path);
+
+ return RS_IO_JOB(prefetch);
+}
Added: branches/rawstudio-ng-color/librawstudio/rs-io-job-prefetch.h
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rs-io-job-prefetch.h
(rev 0)
+++ branches/rawstudio-ng-color/librawstudio/rs-io-job-prefetch.h
2010-01-14 18:11:23 UTC (rev 2989)
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <[email protected]> and
+ * Anders Kvist <[email protected]>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
+ */
+
+#ifndef RS_IO_JOB_PREFETCH_H
+#define RS_IO_JOB_PREFETCH_H
+
+#include <glib-object.h>
+#include "rs-io-job.h"
+
+G_BEGIN_DECLS
+
+#define RS_TYPE_IO_JOB_PREFETCH rs_io_job_prefetch_get_type()
+#define RS_IO_JOB_PREFETCH(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),
RS_TYPE_IO_JOB_PREFETCH, RSIoJobPrefetch))
+#define RS_IO_JOB_PREFETCH_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass),
RS_TYPE_IO_JOB_PREFETCH, RSIoJobPrefetchClass))
+#define RS_IS_IO_JOB_PREFETCH(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj),
RS_TYPE_IO_JOB_PREFETCH))
+#define RS_IS_IO_JOB_PREFETCH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),
RS_TYPE_IO_JOB_PREFETCH))
+#define RS_IO_JOB_PREFETCH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),
RS_TYPE_IO_JOB_PREFETCH, RSIoJobPrefetchClass))
+
+typedef struct {
+ RSIoJobClass parent_class;
+} RSIoJobPrefetchClass;
+
+GType rs_io_job_prefetch_get_type(void);
+
+RSIoJob *rs_io_job_prefetch_new(const gchar *path);
+
+G_END_DECLS
+
+#endif /* RS_IO_JOB_PREFETCH_H */
Added: branches/rawstudio-ng-color/librawstudio/rs-io-job.c
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rs-io-job.c
(rev 0)
+++ branches/rawstudio-ng-color/librawstudio/rs-io-job.c 2010-01-14
18:11:23 UTC (rev 2989)
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <[email protected]> and
+ * Anders Kvist <[email protected]>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
+ */
+
+#include "rs-io-job.h"
+
+G_DEFINE_TYPE(RSIoJob, rs_io_job, G_TYPE_OBJECT)
+
+static void
+rs_io_job_class_init(RSIoJobClass *klass)
+{
+}
+
+static void
+rs_io_job_init(RSIoJob *job)
+{
+}
+
+RSIoJob *
+rs_io_job_new(void)
+{
+ return g_object_new(RS_TYPE_IO_JOB, NULL);
+}
+
+void
+rs_io_job_execute(RSIoJob *job)
+{
+ g_assert(RS_IS_IO_JOB(job));
+
+ RSIoJobClass *klass = RS_IO_JOB_GET_CLASS(job);
+
+ if (klass->execute)
+ klass->execute(job);
+}
+
+void
+rs_io_job_do_callback(RSIoJob *job)
+{
+ g_assert(RS_IS_IO_JOB(job));
+
+ RSIoJobClass *klass = RS_IO_JOB_GET_CLASS(job);
+
+ if (klass->do_callback)
+ klass->do_callback(job);
+}
Added: branches/rawstudio-ng-color/librawstudio/rs-io-job.h
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rs-io-job.h
(rev 0)
+++ branches/rawstudio-ng-color/librawstudio/rs-io-job.h 2010-01-14
18:11:23 UTC (rev 2989)
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <[email protected]> and
+ * Anders Kvist <[email protected]>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
+ */
+
+#ifndef RS_IO_JOB_H
+#define RS_IO_JOB_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define RS_TYPE_IO_JOB rs_io_job_get_type()
+#define RS_IO_JOB(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RS_TYPE_IO_JOB,
RSIoJob))
+#define RS_IO_JOB_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass),
RS_TYPE_IO_JOB, RSIoJobClass))
+#define RS_IS_IO_JOB(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RS_TYPE_IO_JOB))
+#define RS_IS_IO_JOB_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),
RS_TYPE_IO_JOB))
+#define RS_IO_JOB_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),
RS_TYPE_IO_JOB, RSIoJobClass))
+
+typedef struct {
+ GObject parent;
+
+ gint idle_class;
+ gint priority;
+ gpointer user_data;
+} RSIoJob;
+
+typedef struct {
+ GObjectClass parent_class;
+
+ void (*execute)(RSIoJob *job);
+ void (*do_callback)(RSIoJob *job);
+} RSIoJobClass;
+
+GType rs_io_job_get_type(void);
+
+RSIoJob *rs_io_job_new(void);
+
+void rs_io_job_execute(RSIoJob *job);
+
+void rs_io_job_do_callback(RSIoJob *job);
+
+G_END_DECLS
+
+#endif /* RS_IO_JOB_H */
Added: branches/rawstudio-ng-color/librawstudio/rs-io.c
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rs-io.c
(rev 0)
+++ branches/rawstudio-ng-color/librawstudio/rs-io.c 2010-01-14 18:11:23 UTC
(rev 2989)
@@ -0,0 +1,249 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <[email protected]> and
+ * Anders Kvist <[email protected]>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
+ */
+
+#include "rs-io.h"
+
+static GStaticMutex init_lock = G_STATIC_MUTEX_INIT;
+static GAsyncQueue *queue = NULL;
+static GThreadPool *callback_pool = NULL;
+static GStaticMutex io_lock = G_STATIC_MUTEX_INIT;
+
+static void
+callback_worker(gpointer data, gpointer user_data)
+{
+ RSIoJob *job = data;
+
+ rs_io_job_do_callback(job);
+
+ g_object_unref(job);
+}
+
+static gint
+queue_sort(gconstpointer a, gconstpointer b, gpointer user_data)
+{
+ gint id1 = 0;
+ gint id2 = 0;
+
+ if (a)
+ id1 = RS_IO_JOB(a)->priority;
+ if (b)
+ id2 = RS_IO_JOB(b)->priority;
+
+ return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
+}
+
+static gpointer
+queue_worker(gpointer data)
+{
+ GAsyncQueue *queue = data;
+ RSIoJob *job;
+
+ while (1)
+ {
+ job = g_async_queue_pop(queue);
+
+ /* If we somehow got NULL, continue. I'm not sure this will
ever happen, but this is better than random segfaults :) */
+ if (!job)
+ continue;
+
+ rs_io_job_execute(job);
+
+ g_thread_pool_push(callback_pool, job, NULL);
+ }
+
+ return NULL;
+}
+
+static void
+init()
+{
+ g_static_mutex_lock(&init_lock);
+ if (!queue)
+ {
+ queue = g_async_queue_new();
+ g_thread_create_full(queue_worker, queue, 0, FALSE, FALSE,
G_THREAD_PRIORITY_LOW, NULL);
+ callback_pool = g_thread_pool_new(callback_worker, NULL,
rs_get_number_of_processor_cores(), TRUE, NULL);
+ }
+ g_static_mutex_unlock(&init_lock);
+}
+
+/**
+ * Add a RSIoJob to be executed later
+ * @param job A RSIoJob. This will be unreffed upon completion
+ * @param idle_class A user defined variable, this can be used with
rs_io_idle_cancel_class() to cancel a batch of queued reads
+ * @param priority Lower value means higher priority
+ * @param user_data A pointer to pass to the callback
+ */
+void
+rs_io_idle_add_job(RSIoJob *job, gint idle_class, gint priority, gpointer
user_data)
+{
+ g_assert(RS_IS_IO_JOB(job));
+
+ job->idle_class = idle_class;
+ job->priority = priority;
+ job->user_data = user_data;
+
+ g_async_queue_push_sorted(queue, job, queue_sort, NULL);
+}
+
+/**
+ * Prefetch a file
+ * @param path Absolute path to a file to prefetch
+ * @param idle_class A user defined variable, this can be used with
rs_io_idle_cancel_class() to cancel a batch of queued reads
+ * @return A pointer to a RSIoJob, this can be used with rs_io_idle_cancel()
+ */
+const RSIoJob *
+rs_io_idle_prefetch_file(const gchar *path, gint idle_class)
+{
+ init();
+
+ RSIoJob *job = rs_io_job_prefetch_new(path);
+ rs_io_idle_add_job(job, idle_class, 20, NULL);
+
+ return job;
+}
+
+/**
+ * Load metadata belonging to a photo
+ * @param path Absolute path to a photo
+ * @param idle_class A user defined variable, this can be used with
rs_io_idle_cancel_class() to cancel a batch of queued reads
+ * @param callback A callback to call when the data is ready or NULL
+ * @param user_data Data to pass to the callback
+ * @return A pointer to a RSIoJob, this can be used with rs_io_idle_cancel()
+ */
+const RSIoJob *
+rs_io_idle_read_metadata(const gchar *path, gint idle_class, RSGotMetadataCB
callback, gpointer user_data)
+{
+ init();
+
+ RSIoJob *job = rs_io_job_metadata_new(path, callback);
+ rs_io_idle_add_job(job, idle_class, 10, user_data);
+
+ return job;
+}
+
+/**
+ * Compute a "Rawstudio checksum" of a file
+ * @param path Absolute path to a file
+ * @param idle_class A user defined variable, this can be used with
rs_io_idle_cancel_class() to cancel a batch of queued reads
+ * @param callback A callback to call when the data is ready or NULL
+ * @param user_data Data to pass to the callback
+ * @return A pointer to a RSIoJob, this can be used with rs_io_idle_cancel()
+ */
+const RSIoJob *
+rs_io_idle_read_checksum(const gchar *path, gint idle_class, RSGotChecksumCB
callback, gpointer user_data)
+{
+ init();
+
+ RSIoJob *job = rs_io_job_checksum_new(path, callback);
+ rs_io_idle_add_job(job, idle_class, 30, user_data);
+
+ return job;
+}
+
+/**
+ * Cancel a complete class of idle requests
+ * @param idle_class The class identifier
+ */
+void
+rs_io_idle_cancel_class(gint idle_class)
+{
+ /* This behaves like rs_io_idle_cancel_class(), please see comments
there */
+ RSIoJob *current_job;
+ RSIoJob *marker_job = rs_io_job_new();
+
+ init();
+
+ g_async_queue_lock(queue);
+
+ /* Put a marker in the queue, we will rotate the complete queue, so we
have to know when we're around */
+ g_async_queue_push_unlocked(queue, marker_job);
+
+ while(current_job = g_async_queue_pop_unlocked(queue))
+ {
+ /* If current job matches marker, we're done */
+ if (current_job == marker_job)
+ break;
+
+ /* Of the job's idle_class doesn't match the class to cancel,
we put the job back in the queue */
+ if (current_job->idle_class != idle_class)
+ g_async_queue_push_unlocked(queue, current_job);
+ }
+
+ /* Make sure the queue is sorted */
+ g_async_queue_sort_unlocked(queue, queue_sort, NULL);
+
+ g_async_queue_unlock(queue);
+
+ g_object_unref(marker_job);
+}
+
+/**
+ * Cancel an idle request
+ * @param request_id A request_id as returned by
rs_io_idle_read_complete_file()
+ */
+void
+rs_io_idle_cancel(RSIoJob *job)
+{
+ /* This behaves like rs_io_idle_cancel_class(), please see comments
there */
+ RSIoJob *current_job;
+ RSIoJob *marker_job = rs_io_job_new();
+
+ init();
+
+ g_async_queue_lock(queue);
+
+ /* Put a marker in the queue, we will rotate the complete queue, so we
have to know when we're around */
+ g_async_queue_push_unlocked(queue, marker_job);
+
+ while(current_job = g_async_queue_pop_unlocked(queue))
+ {
+ /* If current job matches marker, we're done */
+ if (current_job == marker_job)
+ break;
+
+ if (current_job != job)
+ g_async_queue_push_unlocked(queue, current_job);
+ }
+
+ /* Make sure the queue is sorted */
+ g_async_queue_sort_unlocked(queue, queue_sort, NULL);
+
+ g_async_queue_unlock(queue);
+
+ g_object_unref(marker_job);
+}
+
+/**
+ * Aquire the IO lock
+ */
+void
+rs_io_lock()
+{
+ g_static_mutex_lock(&io_lock);
+}
+
+/**
+ * Release the IO lock
+ */
+void
+rs_io_unlock()
+{
+ g_static_mutex_unlock(&io_lock);
+}
Added: branches/rawstudio-ng-color/librawstudio/rs-io.h
===================================================================
--- branches/rawstudio-ng-color/librawstudio/rs-io.h
(rev 0)
+++ branches/rawstudio-ng-color/librawstudio/rs-io.h 2010-01-14 18:11:23 UTC
(rev 2989)
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2006-2009 Anders Brander <[email protected]> and
+ * Anders Kvist <[email protected]>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
+ */
+
+#include <rawstudio.h>
+
+#ifndef RS_IO_H
+#define RS_IO_H
+
+/**
+ * Add a RSIoJob to be executed later
+ * @param job A RSIoJob. This will be unreffed upon completion
+ * @param idle_class A user defined variable, this can be used with
rs_io_idle_cancel_class() to cancel a batch of queued reads
+ * @param priority Lower value means higher priority
+ * @param user_data A pointer to pass to the callback
+ */
+void
+rs_io_idle_add_job(RSIoJob *job, gint idle_class, gint priority, gpointer
user_data);
+
+/**
+ * Prefetch a file
+ * @param path Absolute path to a file to prefetch
+ * @param idle_class A user defined variable, this can be used with
rs_io_idle_cancel_class() to cancel a batch of queued reads
+ * @return A pointer to a RSIoJob, this can be used with rs_io_idle_cancel()
+ */
+const RSIoJob *
+rs_io_idle_prefetch_file(const gchar *path, gint idle_class);
+
+/**
+ * Load metadata belonging to a photo
+ * @param path Absolute path to a photo
+ * @param idle_class A user defined variable, this can be used with
rs_io_idle_cancel_class() to cancel a batch of queued reads
+ * @param callback A callback to call when the data is ready or NULL
+ * @param user_data Data to pass to the callback
+ * @return A pointer to a RSIoJob, this can be used with rs_io_idle_cancel()
+ */
+const RSIoJob *
+rs_io_idle_read_metadata(const gchar *path, gint idle_class, RSGotMetadataCB
callback, gpointer user_data);
+
+/**
+ * Compute a "Rawstudio checksum" of a file
+ * @param path Absolute path to a file
+ * @param idle_class A user defined variable, this can be used with
rs_io_idle_cancel_class() to cancel a batch of queued reads
+ * @param callback A callback to call when the data is ready or NULL
+ * @param user_data Data to pass to the callback
+ * @return A pointer to a RSIoJob, this can be used with rs_io_idle_cancel()
+ */
+const RSIoJob *
+rs_io_idle_read_checksum(const gchar *path, gint idle_class, RSGotChecksumCB
callback, gpointer user_data);
+
+/**
+ * Cancel a complete class of idle requests
+ * @param idle_class The class identifier
+ */
+void
+rs_io_idle_cancel_class(gint idle_class);
+
+/**
+ * Cancel a idle request
+ * @param request_id A request_id as returned by
rs_io_idle_read_complete_file()
+ */
+void
+rs_io_idle_cancel(RSIoJob *job);
+
+/**
+ * Aquire the IO lock
+ */
+void
+rs_io_lock();
+
+/**
+ * Release the IO lock
+ */
+void
+rs_io_unlock();
+
+#endif /* RS_IO_H */
_______________________________________________
Rawstudio-commit mailing list
[email protected]
http://rawstudio.org/cgi-bin/mailman/listinfo/rawstudio-commit