diff options
author | Harry Jeffery <harry@exec64.co.uk> | 2019-02-04 02:34:46 +0000 |
---|---|---|
committer | Harry Jeffery <harry@exec64.co.uk> | 2019-02-04 03:48:03 +0000 |
commit | 05db4c452cbf6d314be60b86524629ef22798adf (patch) | |
tree | af4830b00a8ccfd70a9d5563ef0534924da080ca /src/backend_libpng.c | |
parent | 09928120e31edbfa63791f0c2f5e14374ae518de (diff) | |
download | imv-05db4c452cbf6d314be60b86524629ef22798adf.tar.gz |
Make sources thread-safe
Diffstat (limited to 'src/backend_libpng.c')
-rw-r--r-- | src/backend_libpng.c | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/backend_libpng.c b/src/backend_libpng.c index 2c3b1d7..3713ae8 100644 --- a/src/backend_libpng.c +++ b/src/backend_libpng.c @@ -18,6 +18,7 @@ struct private { static void source_free(struct imv_source *src) { + pthread_mutex_lock(&src->busy); free(src->name); src->name = NULL; @@ -30,6 +31,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); } @@ -57,6 +60,7 @@ static void report_error(struct imv_source *src) msg.bitmap = NULL; msg.error = "Internal error"; + pthread_mutex_unlock(&src->busy); src->callback(&msg); } @@ -76,16 +80,22 @@ 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; if (setjmp(png_jmpbuf(private->png))) { report_error(src); - return; + return -1; } png_bytep *rows = alloca(sizeof(png_bytep) * src->height); @@ -98,13 +108,14 @@ static void load_image(struct imv_source *src) if (setjmp(png_jmpbuf(private->png))) { free(rows[0]); report_error(src); - return; + return -1; } png_read_image(private->png, rows); fclose(private->file); private->file = NULL; send_bitmap(src, rows[0]); + return 0; } static enum backend_result open_path(const char *path, struct imv_source **src) @@ -169,6 +180,7 @@ static enum backend_result open_path(const char *path, struct imv_source **src) source->height = png_get_image_height(private->png, private->info); 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; |