| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 
 | /* Write an image to a disc file. 
 * 
 * 19/3/10
 * 	- from im_wbuffer.c
 * 	- move on top of VipsThreadpool, instead of im_threadgroup_t
 * 23/6/10
 * 	- better buffer handling for single-line images
 * 17/7/10
 * 	- we could get stuck if allocate failed (thanks Tim)
 * 23/2/12
 * 	- we could deadlock if generate failed
 */
/*
    This file is part of VIPS.
    
    VIPS is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/*
    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
 */
/*
#define VIPS_DEBUG
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /*HAVE_UNISTD_H*/
#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/thread.h>
#include <vips/threadpool.h>
#include <vips/debug.h>
#include "sink.h"
/* A buffer we are going to write to disc in a background thread.
 */
typedef struct _WriteBuffer {
	struct _Write *write;
	VipsRegion *region;	/* Pixels */
	VipsRect area;		/* Part of image this region covers */
        VipsSemaphore go; 	/* Start bg thread loop */
        VipsSemaphore nwrite; 	/* Number of threads writing to region */
        VipsSemaphore done; 	/* Bg thread has done write */
        int write_errno;	/* Save write errors here */
	GThread *thread;	/* BG writer thread */
	gboolean kill;		/* Set to ask thread to exit */
} WriteBuffer;
/* Per-call state.
 */
typedef struct _Write {
	SinkBase sink_base;
	/* We are current writing tiles to buf, buf_back is in the hands of
	 * the bg write thread.
	 */
	WriteBuffer *buf;
	WriteBuffer *buf_back;
	/* The file format write operation.
	 */
	VipsRegionWrite write_fn;	
	void *a;		
} Write;
/* Our per-thread state ... we need to also track the buffer that pos is
 * supposed to write to.
 */
typedef struct _WriteThreadState {
	VipsThreadState parent_object;
        WriteBuffer *buf;
} WriteThreadState;
typedef struct _WriteThreadStateClass {
	VipsThreadStateClass parent_class;
} WriteThreadStateClass;
G_DEFINE_TYPE( WriteThreadState, write_thread_state, VIPS_TYPE_THREAD_STATE );
static void
write_thread_state_class_init( WriteThreadStateClass *class )
{
	VipsObjectClass *object_class = VIPS_OBJECT_CLASS( class );
	object_class->nickname = "writethreadstate";
	object_class->description = _( "per-thread state for sinkdisc" );
}
static void
write_thread_state_init( WriteThreadState *state )
{
	state->buf = NULL;
}
static VipsThreadState *
write_thread_state_new( VipsImage *im, void *a )
{
	return( VIPS_THREAD_STATE( vips_object_new( 
		write_thread_state_get_type(), 
		vips_thread_state_set, im, a ) ) );
}
static void
wbuffer_free( WriteBuffer *wbuffer )
{
        /* Is there a thread running this region? Kill it!
         */
        if( wbuffer->thread ) {
                wbuffer->kill = TRUE;
		vips_semaphore_up( &wbuffer->go );
		/* Return value is always NULL (see wbuffer_write_thread).
		 */
		(void) g_thread_join( wbuffer->thread );
		VIPS_DEBUG_MSG( "wbuffer_free: g_thread_join()\n" );
		wbuffer->thread = NULL;
        }
	VIPS_UNREF( wbuffer->region );
	vips_semaphore_destroy( &wbuffer->go );
	vips_semaphore_destroy( &wbuffer->nwrite );
	vips_semaphore_destroy( &wbuffer->done );
	vips_free( wbuffer );
}
static void
wbuffer_write( WriteBuffer *wbuffer )
{
	Write *write = wbuffer->write;
	VIPS_DEBUG_MSG( "wbuffer_write: %d bytes from wbuffer %p\n", 
		wbuffer->region->bpl * wbuffer->area.height, wbuffer );
	wbuffer->write_errno = write->write_fn( wbuffer->region, 
		&wbuffer->area, write->a );
}
#ifdef HAVE_THREADS
/* Run this as a thread to do a BG write.
 */
static void *
wbuffer_write_thread( void *data )
{
	WriteBuffer *wbuffer = (WriteBuffer *) data;
	for(;;) {
		/* Wait to be told to write.
		 */
		vips_semaphore_down( &wbuffer->go );
		if( wbuffer->kill )
			break;
		/* Now block until the last worker finishes on this buffer.
		 */
		vips_semaphore_downn( &wbuffer->nwrite, 0 );
		wbuffer_write( wbuffer );
		/* Signal write complete.
		 */
		vips_semaphore_up( &wbuffer->done );
	}
	return( NULL );
}
#endif /*HAVE_THREADS*/
static WriteBuffer *
wbuffer_new( Write *write )
{
	WriteBuffer *wbuffer;
	if( !(wbuffer = VIPS_NEW( NULL, WriteBuffer )) )
		return( NULL );
	wbuffer->write = write;
	wbuffer->region = NULL;
	vips_semaphore_init( &wbuffer->go, 0, "go" );
	vips_semaphore_init( &wbuffer->nwrite, 0, "nwrite" );
	vips_semaphore_init( &wbuffer->done, 0, "done" );
	wbuffer->write_errno = 0;
	wbuffer->thread = NULL;
	wbuffer->kill = FALSE;
	if( !(wbuffer->region = vips_region_new( write->sink_base.im )) ) {
		wbuffer_free( wbuffer );
		return( NULL );
	}
	/* The worker threads need to be able to move the buffers around.
	 */
	vips__region_no_ownership( wbuffer->region );
#ifdef HAVE_THREADS
	/* Make this last (picks up parts of wbuffer on startup).
	 */
	if( !(wbuffer->thread = g_thread_create( wbuffer_write_thread, wbuffer, 
		TRUE, NULL )) ) {
		vips_error( "wbuffer_new", 
			"%s", _( "unable to create thread" ) );
		wbuffer_free( wbuffer );
		return( NULL );
	}
#endif /*HAVE_THREADS*/
	return( wbuffer );
}
/* Block until the previous write completes, then write the front buffer.
 */
static int
wbuffer_flush( Write *write )
{
	VIPS_DEBUG_MSG( "wbuffer_flush:\n" );
	/* Block until the other buffer has been written. We have to do this 
	 * before we can set this buffer writing or we'll lose output ordering.
	 */
	if( write->buf->area.top > 0 ) {
		vips_semaphore_down( &write->buf_back->done );
		/* Previous write suceeded?
		 */
		if( write->buf_back->write_errno ) {
			vips_error_system( write->buf_back->write_errno,
				"wbuffer_write", "%s", _( "write failed" ) );
			return( -1 ); 
		}
	}
	/* Set the background writer going for this buffer.
	 */
#ifdef HAVE_THREADS
	vips_semaphore_up( &write->buf->go );
#else
	/* No threads? Write ourselves synchronously.
	 */
	wbuffer_write( write->buf );
#endif /*HAVE_THREADS*/
	return( 0 );
}
/* Move a wbuffer to a position.
 */
static int 
wbuffer_position( WriteBuffer *wbuffer, int top, int height )
{
	VipsRect image, area;
	int result;
	image.left = 0;
	image.top = 0;
	image.width = wbuffer->write->sink_base.im->Xsize;
	image.height = wbuffer->write->sink_base.im->Ysize;
	area.left = 0;
	area.top = top;
	area.width = wbuffer->write->sink_base.im->Xsize;
	area.height = height;
	vips_rect_intersectrect( &area, &image, &wbuffer->area );
	/* The workers take turns to move the buffers.
	 */
	vips__region_take_ownership( wbuffer->region );
	result = vips_region_buffer( wbuffer->region, &wbuffer->area );
	vips__region_no_ownership( wbuffer->region );
	/* This should be an exclusive buffer, hopefully.
	 */
	g_assert( !wbuffer->region->buffer->done );
	return( result );
}
/* Our VipsThreadpoolAllocate function ... move the thread to the next tile
 * that needs doing. If no buffer is available (the bg writer hasn't yet
 * finished with it), we block. If all tiles are done, we return FALSE to end
 * iteration.
 */
static gboolean
wbuffer_allocate_fn( VipsThreadState *state, void *a, gboolean *stop )
{
	WriteThreadState *wstate =  (WriteThreadState *) state;
	Write *write = (Write *) a;
	SinkBase *sink_base = (SinkBase *) write;
	VipsRect image;
	VipsRect tile;
	VIPS_DEBUG_MSG( "wbuffer_allocate_fn:\n" );
	/* Is the state x/y OK? New line or maybe new buffer or maybe even 
	 * all done.
	 */
	if( sink_base->x >= write->buf->area.width ) {
		sink_base->x = 0;
		sink_base->y += sink_base->tile_height;
		if( sink_base->y >= VIPS_RECT_BOTTOM( &write->buf->area ) ) {
			/* Block until the last write is done, then set write
			 * of the front buffer going.
			 */
			if( wbuffer_flush( write ) )
				return( -1 );
			/* End of image?
			 */
			if( sink_base->y >= sink_base->im->Ysize ) {
				*stop = TRUE;
				return( 0 );
			}
			VIPS_DEBUG_MSG( "wbuffer_allocate_fn: "
				"finished top = %d, height = %d\n",
				write->buf->area.top, write->buf->area.height );
			VIPS_DEBUG_MSG( "wbuffer_allocate_fn: "
				"starting top = %d, height = %d\n",
				sink_base->y, sink_base->nlines );
			/* Swap buffers.
			 */
			VIPS_SWAP( WriteBuffer *, write->buf, write->buf_back );
			/* Position buf at the new y.
			 */
			if( wbuffer_position( write->buf, 
				sink_base->y, sink_base->nlines ) )
				return( -1 );
		}
	}
	/* x, y and buf are good: save params for thread.
	 */
	image.left = 0;
	image.top = 0;
	image.width = sink_base->im->Xsize;
	image.height = sink_base->im->Ysize;
	tile.left = sink_base->x;
	tile.top = sink_base->y;
	tile.width = sink_base->tile_width;
	tile.height = sink_base->tile_height;
	vips_rect_intersectrect( &image, &tile, &state->pos );
	/* The thread needs to know which buffer it's writing to.
	 */
	wstate->buf = write->buf;
	VIPS_DEBUG_MSG( "  allocated %d x %d:\n", tile.left, tile.top );
	/* Add to the number of writers on the buffer.
	 */
	vips_semaphore_upn( &write->buf->nwrite, -1 );
	/* Move state on.
	 */
	sink_base->x += sink_base->tile_width;
	/* Add the number of pixels we've just allocated to progress.
	 */
	sink_base->processed += state->pos.width * state->pos.height;
	return( 0 );
}
/* Our VipsThreadpoolWork function ... generate a tile!
 */
static int
wbuffer_work_fn( VipsThreadState *state, void *a )
{
	WriteThreadState *wstate = (WriteThreadState *) state;
	int result;
	VIPS_DEBUG_MSG( "wbuffer_work_fn: %p %d x %d\n", 
		state, state->pos.left, state->pos.top );
	result = vips_region_prepare_to( state->reg, wstate->buf->region, 
		&state->pos, state->pos.left, state->pos.top );
	VIPS_DEBUG_MSG( "wbuffer_work_fn: %p result = %d\n", state, result );
	/* Tell the bg write thread we've left.
	 */
	vips_semaphore_upn( &wstate->buf->nwrite, 1 );
	return( result );
}
static void
write_init( Write *write, 
	VipsImage *image, VipsRegionWrite write_fn, void *a )
{
	vips_sink_base_init( &write->sink_base, image );
	write->buf = wbuffer_new( write );
	write->buf_back = wbuffer_new( write );
	write->write_fn = write_fn;
	write->a = a;
}
static void
write_free( Write *write )
{
	VIPS_FREEF( wbuffer_free, write->buf );
	VIPS_FREEF( wbuffer_free, write->buf_back );
}
/**
 * VipsRegionWrite:
 * @region: get pixels from here
 * @area: area to write
 * @a: client data
 *
 * The function should write the pixels in @area from @region. @a is the 
 * value passed into vips_discsink().
 *
 * See also: vips_sink_disc().
 *
 * Returns: 0 on success, -1 on error.
 */
/**
 * vips_sink_disc:
 * @im: image to process
 * @write_fn: called for every batch of pixels
 * @a: client data
 *
 * vips_sink_disc() loops over @im, top-to-bottom, generating it in sections.
 * As each section is produced, @write_fn is called.
 *
 * @write_fn is always called single-threaded (though not always from the same
 * thread), it's always given image
 * sections in top-to-bottom order, and there are never any gaps.
 *
 * This operation is handy for making image sinks which output to things like 
 * disc files.
 *
 * See also: vips_concurrency_set().
 *
 * Returns: 0 on success, -1 on error.
 */
int
vips_sink_disc( VipsImage *im, VipsRegionWrite write_fn, void *a )
{
	Write write;
	int result;
	vips_image_preeval( im );
	write_init( &write, im, write_fn, a );
	result = 0;
	if( !write.buf || 
		!write.buf_back || 
		wbuffer_position( write.buf, 0, write.sink_base.nlines ) ||
		vips_threadpool_run( im, 
			write_thread_state_new, 
			wbuffer_allocate_fn, 
			wbuffer_work_fn, 
			vips_sink_base_progress, 
			&write ) )  
		result = -1;
	/* Just before allocate signalled stop, it set write.buf writing. We
	 * need to wait for this write to finish. 
	 *
	 * We can't just free the buffers (which will wait for the bg threads 
	 * to finish), since the bg thread might see the kill before it gets a 
	 * chance to write.
	 *
	 * If the pool exited with an error, write.buf might not have been
	 * started (if the allocate failed), and in any case, we don't care if
	 * the final write went through or not.
	 */
	if( !result )
		vips_semaphore_down( &write.buf->done );
	vips_image_posteval( im );
	write_free( &write );
	return( result );
}
 |