count threads for leak test

see https://github.com/jcupitt/libvips/issues/542
This commit is contained in:
John Cupitt 2016-10-19 10:52:18 +01:00
parent 9f97d7b4ff
commit 26a534eeac
6 changed files with 45 additions and 6 deletions

View File

@ -102,6 +102,8 @@ extern char *vips__cache_max_files;
extern gboolean vips__cache_dump;
extern gboolean vips__cache_trace;
extern int vips__n_active_threads;
void vips__cache_init( void );
void vips__print_renders( void );

View File

@ -50,6 +50,7 @@ void vips_g_cond_free( GCond * );
/* ... and for GThread.
*/
GThread *vips_g_thread_new( const char *, GThreadFunc, gpointer );
void *vips_g_thread_join( GThread *thread );
gboolean vips_thread_isworker( void );

View File

@ -481,6 +481,10 @@ vips_leak( void )
vips_buf_appendf( &buf, "error buffer: %s",
vips_error_buffer() );
if( vips__n_active_threads != 0 )
vips_buf_appendf( &buf, "threads: %d still active\n",
vips__n_active_threads );
fprintf( stderr, "%s", vips_buf_all( &buf ) );

View File

@ -147,8 +147,8 @@ wbuffer_free( WriteBuffer *wbuffer )
/* Return value is always NULL (see wbuffer_write_thread).
*/
(void) g_thread_join( wbuffer->thread );
VIPS_DEBUG_MSG( "wbuffer_free: g_thread_join()\n" );
(void) vips_g_thread_join( wbuffer->thread );
VIPS_DEBUG_MSG( "wbuffer_free: vips_g_thread_join()\n" );
wbuffer->thread = NULL;
}

View File

@ -506,7 +506,7 @@ vips__render_shutdown( void )
render_reschedule = TRUE;
render_kill = TRUE;
(void) g_thread_join( thread );
(void) vips_g_thread_join( thread );
}
else
g_mutex_unlock( render_dirty_lock );

View File

@ -100,6 +100,10 @@ int vips__thinstrip_height = VIPS__THINSTRIP_HEIGHT;
*/
int vips__concurrency = 0;
/* Count the number of threads we have active and report on leak test.
*/
int vips__n_active_threads = 0;
/* Set this GPrivate to indicate that this is a vips worker.
*/
static GPrivate vips_threadpool_is_worker_private;
@ -244,9 +248,39 @@ vips_g_thread_new( const char *domain, GThreadFunc func, gpointer data )
"%s", _( "unable to create thread" ) );
}
if( thread &&
vips__leak ) {
g_mutex_lock( vips__global_lock );
vips__n_active_threads += 1;
g_mutex_unlock( vips__global_lock );
}
return( thread );
}
void *
vips_g_thread_join( GThread *thread )
{
void *result;
result = g_thread_join( thread );
VIPS_DEBUG_MSG_RED( "vips_g_thread_join: g_thread_join( %p )\n",
thread );
if( vips__leak ) {
g_mutex_lock( vips__global_lock );
vips__n_active_threads -= 1;
g_mutex_unlock( vips__global_lock );
}
return( result );
}
/**
* vips_concurrency_set:
* @concurrency: number of threads to run
@ -536,9 +570,7 @@ vips_thread_free( VipsThread *thr )
/* Return value is always NULL (see thread_main_loop).
*/
(void) g_thread_join( thr->thread );
VIPS_DEBUG_MSG_RED( "thread_free: g_thread_join( %p )\n",
thr->thread );
(void) vips_g_thread_join( thr->thread );
thr->thread = NULL;
}