From a7edd235c4874438b2952df7602de14c2eb6a777 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Fri, 28 Oct 2016 16:25:31 +0100 Subject: [PATCH] improve compat with ancient glibs and fix a tiny threading leak --- ChangeLog | 2 ++ libvips/include/vips/internal.h | 2 ++ libvips/iofuncs/buffer.c | 2 +- libvips/iofuncs/init.c | 3 +++ libvips/iofuncs/threadpool.c | 29 +++++++++++++++++++++++------ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 941a8f26..90d493d4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 18/10/16 started 8.4.3 - fix error detection in gif_close, thanks aaron42net +- fix tiny threading memleak +- improve compatibility with very old glib, see #548 27/9/16 started 8.4.2 - small doc improvements diff --git a/libvips/include/vips/internal.h b/libvips/include/vips/internal.h index 6d32afa2..8ad38485 100644 --- a/libvips/include/vips/internal.h +++ b/libvips/include/vips/internal.h @@ -102,6 +102,8 @@ extern char *vips__cache_max_files; extern gboolean vips__cache_dump; extern gboolean vips__cache_trace; +void vips__threadpool_init( void ); + void vips__cache_init( void ); void vips__print_renders( void ); diff --git a/libvips/iofuncs/buffer.c b/libvips/iofuncs/buffer.c index 23e450ad..a5af8fe2 100644 --- a/libvips/iofuncs/buffer.c +++ b/libvips/iofuncs/buffer.c @@ -712,7 +712,7 @@ buffer_thread_destroy_notify( VipsBufferThread *buffer_thread ) buffer_thread_free( buffer_thread ); } -/* Init the buffer cache system. +/* Init the buffer cache system. This is called during vips_init. */ void vips__buffer_init( void ) diff --git a/libvips/iofuncs/init.c b/libvips/iofuncs/init.c index fb492808..2a576544 100644 --- a/libvips/iofuncs/init.c +++ b/libvips/iofuncs/init.c @@ -302,6 +302,9 @@ vips_init( const char *argv0 ) g_thread_init( NULL ); #endif + vips__threadpool_init(); + vips__buffer_init(); + /* This does an unsynchronised static hash table init on first call -- * we have to make sure we do this single-threaded. See: * https://github.com/openslide/openslide/issues/161 diff --git a/libvips/iofuncs/threadpool.c b/libvips/iofuncs/threadpool.c index 860690c6..c39b593f 100644 --- a/libvips/iofuncs/threadpool.c +++ b/libvips/iofuncs/threadpool.c @@ -101,7 +101,7 @@ int vips__concurrency = 0; /* Set this GPrivate to indicate that this is a vips worker. */ -static GPrivate vips_threadpool_is_worker_private; +static GPrivate *is_worker_key = NULL; /* Glib 2.32 revised the thread API. We need some compat functions. */ @@ -165,7 +165,7 @@ vips_g_cond_free( GCond *cond ) gboolean vips_thread_isworker( void ) { - return( g_private_get( &vips_threadpool_is_worker_private ) != NULL ); + return( g_private_get( is_worker_key ) != NULL ); } typedef struct { @@ -181,12 +181,12 @@ vips_thread_run( gpointer data ) void *result; - if( vips__thread_profile ) - vips__thread_profile_attach( info->domain ); - /* Set this to something (anything) to tag this thread as a vips worker. */ - g_private_set( &vips_threadpool_is_worker_private, data ); + g_private_set( is_worker_key, data ); + + if( vips__thread_profile ) + vips__thread_profile_attach( info->domain ); result = info->func( info->data ); @@ -940,6 +940,23 @@ vips_threadpool_run( VipsImage *im, return( result ); } +/* Start up threadpools. This is called during vips_init. + */ +void +vips__threadpool_init( void ) +{ + /* We need to work with the pre-2.32 threading API. + */ +#ifdef HAVE_PRIVATE_INIT + static GPrivate private = { 0 }; + + is_worker_key = &private; +#else + if( !is_worker_key ) + is_worker_key = g_private_new( NULL ); +#endif +} + /** * vips_get_tile_size: * @im: image to guess for