Beispiel #1
0
/**
 * vips_cache_operation_add:
 * @operation: (transfer none): pointer to operation to add
 *
 * Add a built operation to the cache. The cache will ref the operation. 
 */
void
vips_cache_operation_add( VipsOperation *operation )
{
	g_assert( VIPS_OBJECT( operation )->constructed ); 

	g_mutex_lock( vips_cache_lock );

	/* If two threads call the same operation at the same time, 
	 * we can get multiple adds. Let the first one win. See
	 * https://github.com/jcupitt/libvips/pull/181
	 */
	if( !g_hash_table_lookup( vips_cache_table, operation ) ) {
		VipsOperationFlags flags = 
			vips_operation_get_flags( operation );
		gboolean nocache = flags & VIPS_OPERATION_NOCACHE;

		/* Has to be after _build() so we can see output args.
		 */
		if( vips__cache_trace ) {
			if( nocache )
				printf( "vips cache : " );
			else
				printf( "vips cache+: " );
			vips_object_print_summary( VIPS_OBJECT( operation ) );
		}

		if( !nocache ) 
			vips_cache_insert( operation );
	}

	g_mutex_unlock( vips_cache_lock );

	vips_cache_trim();
}
Beispiel #2
0
/**
 * vips_cache_operation_buildp: (skip)
 * @operation: pointer to operation to lookup
 *
 * Look up @operation in the cache. If we get a hit, unref @operation, ref the
 * old one and return that through the argument pointer. 
 *
 * If we miss, build and add @operation.
 *
 * Returns: 0 on success, or -1 on error.
 */
int
vips_cache_operation_buildp( VipsOperation **operation )
{
	VipsOperation *hit;

	g_assert( VIPS_IS_OPERATION( *operation ) );

#ifdef VIPS_DEBUG
	printf( "vips_cache_operation_build: " );
	vips_object_print_dump( VIPS_OBJECT( *operation ) );
#endif /*VIPS_DEBUG*/

	vips_cache_trim();

	g_mutex_lock( vips_cache_lock );

	if( (hit = g_hash_table_lookup( vips_cache_table, *operation )) ) {
		if( vips__cache_trace ) {
			printf( "vips cache-: " );
			vips_object_print_summary( VIPS_OBJECT( hit ) );
		}

		/* Ref before unref in case *operation == hit.
		 */
		vips_cache_ref( hit );
		g_object_unref( *operation );

		*operation = hit;
	}

	g_mutex_unlock( vips_cache_lock );

	if( !hit ) {
		if( vips_object_build( VIPS_OBJECT( *operation ) ) ) 
			return( -1 );

		/* Has to be after _build() so we can see output args.
		 */
		if( vips__cache_trace ) {
			if( vips_operation_get_flags( *operation ) & 
				VIPS_OPERATION_NOCACHE )
				printf( "vips cache : " );
			else
				printf( "vips cache+: " );
			vips_object_print_summary( VIPS_OBJECT( *operation ) );
		}

		g_mutex_lock( vips_cache_lock );

		if( !(vips_operation_get_flags( *operation ) & 
			VIPS_OPERATION_NOCACHE) ) {
			vips_cache_ref( *operation );
			g_hash_table_insert( vips_cache_table, 
				*operation, *operation );
		}

		g_mutex_unlock( vips_cache_lock );
	}


	return( 0 );
}