summaryrefslogtreecommitdiffstats
path: root/libdimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-06-22 05:32:04 +0000
committerTavian Barnes <tavianator@gmail.com>2009-06-22 05:32:04 +0000
commitd23bff8a2eee13eec0724aabe5d9a86b126c4b2c (patch)
tree20541946b210329d516e606842745d1504deb8b8 /libdimension
parent3eb41435e3c266a6a8f3496ffa5d832f5951c50f (diff)
downloaddimension-d23bff8a2eee13eec0724aabe5d9a86b126c4b2c.tar.xz
Add interface for performing long tasks in background, while giving a
progress indication.
Diffstat (limited to 'libdimension')
-rw-r--r--libdimension/Makefile.am4
-rw-r--r--libdimension/array.c51
-rw-r--r--libdimension/dimension.h1
-rw-r--r--libdimension/dimension/array.h5
-rw-r--r--libdimension/dimension/progress.h53
-rw-r--r--libdimension/progress.c103
6 files changed, 187 insertions, 30 deletions
diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am
index 2b5784a..d4d01ec 100644
--- a/libdimension/Makefile.am
+++ b/libdimension/Makefile.am
@@ -17,10 +17,10 @@
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
###########################################################################
-nobase_include_HEADERS = dimension.h dimension/array.h dimension/camera.h dimension/canvas.h dimension/color.h dimension/cube.h dimension/error.h dimension/geometry.h dimension/png.h dimension/object.h dimension/scene.h dimension/sphere.h
+nobase_include_HEADERS = dimension.h dimension/array.h dimension/camera.h dimension/canvas.h dimension/color.h dimension/cube.h dimension/error.h dimension/geometry.h dimension/png.h dimension/progress.h dimension/object.h dimension/scene.h dimension/sphere.h
lib_LTLIBRARIES = libdimension.la
-libdimension_la_SOURCES = $(nobase_include_HEADERS) array.c camera.c canvas.c color.c cube.c error.c geometry.c png.c object.c raytrace.c scene.c sphere.c
+libdimension_la_SOURCES = $(nobase_include_HEADERS) array.c camera.c canvas.c color.c cube.c error.c geometry.c png.c progress.c object.c raytrace.c scene.c sphere.c
libdimension_la_LDFLAGS = -version-info 0:0:0
libdimension_la_LIBADD = -lm -lpthread -lpng
diff --git a/libdimension/array.c b/libdimension/array.c
index c106f2f..f0378cf 100644
--- a/libdimension/array.c
+++ b/libdimension/array.c
@@ -25,7 +25,6 @@
/* The raw implementations, which don't do any thread synchronicity */
static void dmnsn_array_get_impl(const dmnsn_array *array, size_t i, void *obj);
static void dmnsn_array_set_impl(dmnsn_array *array, size_t i, const void *obj);
-static void dmnsn_array_resize_impl(dmnsn_array *array, size_t length);
dmnsn_array *
dmnsn_new_array(size_t obj_size)
@@ -73,7 +72,7 @@ dmnsn_array_pop(dmnsn_array *array, void *obj)
{
dmnsn_array_wrlock(array);
dmnsn_array_get_impl(array, array->length - 1, obj);
- dmnsn_array_resize_impl(array, array->length - 1);
+ dmnsn_array_resize_unlocked(array, array->length - 1);
dmnsn_array_unlock(array);
}
@@ -97,30 +96,21 @@ void
dmnsn_array_resize(dmnsn_array *array, size_t length)
{
dmnsn_array_wrlock(array);
- dmnsn_array_resize_impl(array, length);
+ dmnsn_array_resize_unlocked(array, length);
dmnsn_array_unlock(array);
}
-static void
-dmnsn_array_get_impl(const dmnsn_array *array, size_t i, void *obj)
+void *
+dmnsn_array_at(dmnsn_array *array, size_t i)
{
if (i >= array->length) {
dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds.");
}
- memcpy(obj, array->ptr + array->obj_size*i, array->obj_size);
-}
-
-static void
-dmnsn_array_set_impl(dmnsn_array *array, size_t i, const void *obj)
-{
- if (i >= array->length) {
- dmnsn_array_resize_impl(array, i + 1);
- }
- memcpy(array->ptr + array->obj_size*i, obj, array->obj_size);
+ return array->ptr + array->obj_size*i;
}
-static void
-dmnsn_array_resize_impl(dmnsn_array *array, size_t length)
+void
+dmnsn_array_resize_unlocked(dmnsn_array *array, size_t length)
{
if (length > array->capacity) {
array->capacity = length*2; /* We are greedy */
@@ -133,15 +123,6 @@ dmnsn_array_resize_impl(dmnsn_array *array, size_t length)
array->length = length;
}
-void *
-dmnsn_array_at(dmnsn_array *array, size_t i)
-{
- if (i >= array->length) {
- dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds.");
- }
- return array->ptr + array->obj_size*i;
-}
-
void
dmnsn_array_rdlock(const dmnsn_array *array)
{
@@ -173,3 +154,21 @@ dmnsn_array_unlock(const dmnsn_array *array)
dmnsn_error(DMNSN_SEVERITY_MEDIUM, "Couldn't unlock array.");
}
}
+
+static void
+dmnsn_array_get_impl(const dmnsn_array *array, size_t i, void *obj)
+{
+ if (i >= array->length) {
+ dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds.");
+ }
+ memcpy(obj, array->ptr + array->obj_size*i, array->obj_size);
+}
+
+static void
+dmnsn_array_set_impl(dmnsn_array *array, size_t i, const void *obj)
+{
+ if (i >= array->length) {
+ dmnsn_array_resize_unlocked(array, i + 1);
+ }
+ memcpy(array->ptr + array->obj_size*i, obj, array->obj_size);
+}
diff --git a/libdimension/dimension.h b/libdimension/dimension.h
index ba47487..bdf298e 100644
--- a/libdimension/dimension.h
+++ b/libdimension/dimension.h
@@ -28,6 +28,7 @@ extern "C" {
#include <dimension/error.h>
#include <dimension/array.h>
+#include <dimension/progress.h>
#include <dimension/geometry.h>
#include <dimension/color.h>
#include <dimension/canvas.h>
diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h
index 30a119d..5abdf48 100644
--- a/libdimension/dimension/array.h
+++ b/libdimension/dimension/array.h
@@ -48,10 +48,11 @@ void dmnsn_array_set(dmnsn_array *array, size_t i, const void *obj);
void dmnsn_array_resize(dmnsn_array *array, size_t length);
-/* Thread-unsafe! Don't give multiple threads the same raw pointer... */
+/* Manual locking */
+
void *dmnsn_array_at(dmnsn_array *array, size_t i);
+void dmnsn_array_resize_unlocked(dmnsn_array *array, size_t length);
-/* Manual locking */
void dmnsn_array_rdlock(const dmnsn_array *array);
void dmnsn_array_wrlock(dmnsn_array *array);
void dmnsn_array_unlock(const dmnsn_array *array);
diff --git a/libdimension/dimension/progress.h b/libdimension/dimension/progress.h
new file mode 100644
index 0000000..d436b5d
--- /dev/null
+++ b/libdimension/dimension/progress.h
@@ -0,0 +1,53 @@
+/*************************************************************************
+ * Copyright (C) 2008 Tavian Barnes <tavianator@gmail.com> *
+ * *
+ * This file is part of The Dimension Library. *
+ * *
+ * The Dimension Library 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 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Library 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, see *
+ * <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+#ifndef DIMENSION_PROGRESS_H
+#define DIMENSION_PROGRESS_H
+
+#include <pthread.h>
+
+/* A single element in an array for dmnsn_progress. Progress of this item is
+ progress/total. */
+typedef struct {
+ unsigned int progress, total;
+} dmnsn_progress_element;
+
+typedef struct {
+ /* Array of progress elements. Progress is given by P(0), where
+ P(i) = (elements[i].progress + P(i + 1))/elements[i].total. */
+ dmnsn_array *elements;
+
+ /* The worker thread */
+ pthread_t thread;
+} dmnsn_progress;
+
+dmnsn_progress *dmnsn_new_progress();
+void dmnsn_delete_progress(dmnsn_progress *progress);
+
+/* This joins the worker thread and returns it's integer return value in
+ addition to deleting `progress' */
+int dmnsn_finish_progress(dmnsn_progress *progress);
+
+double dmnsn_get_progress(const dmnsn_progress* progress);
+
+void dmnsn_new_progress_element(dmnsn_progress* progress, unsigned int total);
+void dmnsn_increment_progress(dmnsn_progress* progress);
+
+#endif /* DIMENSION_PROGRESS_H */
diff --git a/libdimension/progress.c b/libdimension/progress.c
new file mode 100644
index 0000000..e072f5d
--- /dev/null
+++ b/libdimension/progress.c
@@ -0,0 +1,103 @@
+/*************************************************************************
+ * Copyright (C) 2008 Tavian Barnes <tavianator@gmail.com> *
+ * *
+ * This file is part of The Dimension Library. *
+ * *
+ * The Dimension Library 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 3 of the *
+ * License, or (at your option) any later version. *
+ * *
+ * The Dimension Library 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, see *
+ * <http://www.gnu.org/licenses/>. *
+ *************************************************************************/
+
+#include "dimension.h"
+#include <pthread.h>
+#include <stdlib.h> /* For malloc */
+
+dmnsn_progress *
+dmnsn_new_progress()
+{
+ dmnsn_progress *progress = malloc(sizeof(dmnsn_progress));
+ if (progress) {
+ progress->elements = dmnsn_new_array(sizeof(dmnsn_progress_element));
+ }
+ return progress;
+}
+
+void
+dmnsn_delete_progress(dmnsn_progress *progress)
+{
+ if (progress) {
+ dmnsn_delete_array(progress->elements);
+ free(progress);
+ }
+}
+
+int dmnsn_finish_progress(dmnsn_progress *progress)
+{
+ void *ptr;
+ int retval = 1;
+
+ if (progress) {
+ if (pthread_join(progress->thread, &ptr) != 0) {
+ /* Medium severity because an unjoined thread likely means that the thread
+ is incomplete or invalid */
+ dmnsn_error(DMNSN_SEVERITY_MEDIUM, "Joining worker thread failed.");
+ } else if (ptr) {
+ retval = *(int *)ptr;
+ free(ptr);
+ }
+ dmnsn_delete_progress(progress);
+ }
+
+ return retval;
+}
+
+double
+dmnsn_get_progress(const dmnsn_progress* progress)
+{
+ dmnsn_progress_element *element;
+ double prog = 0.0;
+ unsigned int i;
+
+ dmnsn_array_rdlock(progress->elements);
+ for (i = 0; i < progress->elements->length; ++i) {
+ element = dmnsn_array_at(progress->elements,
+ progress->elements->length - i - 1);
+ prog += element->progress;
+ prog /= element->total;
+ }
+ dmnsn_array_unlock(progress->elements);
+
+ return prog;
+}
+
+void
+dmnsn_new_progress_element(dmnsn_progress* progress, unsigned int total)
+{
+ dmnsn_progress_element element = { .progress = 0, .total = total };
+ dmnsn_array_push(progress->elements, &element);
+}
+
+void
+dmnsn_increment_progress(dmnsn_progress* progress)
+{
+ dmnsn_progress_element *element;
+
+ dmnsn_array_wrlock(progress->elements);
+ element = dmnsn_array_at(progress->elements, progress->elements->length - 1);
+ ++element->progress;
+ if (element->progress >= element->total) {
+ dmnsn_array_resize_unlocked(progress->elements,
+ progress->elements->length - 1);
+ }
+ dmnsn_array_unlock(progress->elements);
+}