aboutsummaryrefslogtreecommitdiff
path: root/src/backend_libjpeg.c
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-02-04 02:34:46 +0000
committerHarry Jeffery <harry@exec64.co.uk>2019-02-04 03:48:03 +0000
commit05db4c452cbf6d314be60b86524629ef22798adf (patch)
treeaf4830b00a8ccfd70a9d5563ef0534924da080ca /src/backend_libjpeg.c
parent09928120e31edbfa63791f0c2f5e14374ae518de (diff)
downloadimv-05db4c452cbf6d314be60b86524629ef22798adf.tar.gz
Make sources thread-safe
Diffstat (limited to 'src/backend_libjpeg.c')
-rw-r--r--src/backend_libjpeg.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/backend_libjpeg.c b/src/backend_libjpeg.c
index 3fde6c2..e83ddc3 100644
--- a/src/backend_libjpeg.c
+++ b/src/backend_libjpeg.c
@@ -21,6 +21,7 @@ struct private {
static void source_free(struct imv_source *src)
{
+ pthread_mutex_lock(&src->busy);
free(src->name);
src->name = NULL;
@@ -37,6 +38,8 @@ static void source_free(struct imv_source *src)
free(src->private);
src->private = NULL;
+ pthread_mutex_unlock(&src->busy);
+ pthread_mutex_destroy(&src->busy);
free(src);
}
@@ -64,6 +67,7 @@ static void report_error(struct imv_source *src)
msg.bitmap = NULL;
msg.error = "Internal error";
+ pthread_mutex_unlock(&src->busy);
src->callback(&msg);
}
@@ -82,24 +86,31 @@ static void send_bitmap(struct imv_source *src, void *bitmap)
msg.frametime = 0;
msg.error = NULL;
+ pthread_mutex_unlock(&src->busy);
src->callback(&msg);
}
-static void load_image(struct imv_source *src)
+static int load_image(struct imv_source *src)
{
+ /* Don't run if this source is already active */
+ if (pthread_mutex_trylock(&src->busy)) {
+ return -1;
+ }
+
struct private *private = src->private;
void *bitmap = malloc(src->height * src->width * 4);
int rcode = tjDecompress2(private->jpeg, private->data, private->len,
bitmap, src->width, 0, src->height, TJPF_RGBA, TJFLAG_FASTDCT);
- if (!rcode) {
- send_bitmap(src, bitmap);
- } else {
+ if (rcode) {
free(bitmap);
report_error(src);
- return;
+ return -1;
}
+
+ send_bitmap(src, bitmap);
+ return 0;
}
static enum backend_result open_path(const char *path, struct imv_source **src)
@@ -148,6 +159,7 @@ static enum backend_result open_path(const char *path, struct imv_source **src)
source->height = height;
source->num_frames = 1;
source->next_frame = 1;
+ pthread_mutex_init(&source->busy, NULL);
source->load_first_frame = &load_image;
source->load_next_frame = NULL;
source->free = &source_free;
@@ -187,6 +199,7 @@ static enum backend_result open_memory(void *data, size_t len, struct imv_source
source->height = height;
source->num_frames = 1;
source->next_frame = 1;
+ pthread_mutex_init(&source->busy, NULL);
source->load_first_frame = &load_image;
source->load_next_frame = NULL;
source->free = &source_free;