Replace the pipe with the ring buffer.

This commit is contained in:
Vreixo Formoso
2007-12-23 02:46:35 +01:00
parent 94e687d9d3
commit 6154fb1f0a
2 changed files with 29 additions and 34 deletions

View File

@ -22,7 +22,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include <unistd.h>
static static
void ecma119_image_free(Ecma119Image *t) void ecma119_image_free(Ecma119Image *t)
@ -40,6 +39,7 @@ void ecma119_image_free(Ecma119Image *t)
} }
free(t->input_charset); free(t->input_charset);
free(t->writers); free(t->writers);
free(t->buffer);
free(t); free(t);
} }
@ -596,13 +596,13 @@ void *write_function(void *arg)
} }
} }
close(target->wrfd); iso_ring_buffer_writer_close(target->buffer);
pthread_exit(NULL); pthread_exit(NULL);
write_error:; write_error:;
iso_msg_fatal(target->image, LIBISO_WRITE_ERROR, iso_msg_fatal(target->image, LIBISO_WRITE_ERROR,
"Image write error, code %d", res); "Image write error, code %d", res);
close(target->wrfd); iso_ring_buffer_writer_close(target->buffer);
pthread_exit(NULL); pthread_exit(NULL);
} }
@ -693,11 +693,9 @@ int ecma119_image_new(IsoImage *src, Ecma119WriteOpts *opts,
/* 4. Create and start writting thread */ /* 4. Create and start writting thread */
/* TODO for now, a pipe to comunicate both threads /* create the ring buffer */
* TODO replace it with a ring buffer */ ret = iso_ring_buffer_new(&target->buffer);
ret = pipe(&(target->rdfd));
if (ret < 0) { if (ret < 0) {
ret = ISO_ERROR;
goto target_cleanup; goto target_cleanup;
} }
@ -732,24 +730,20 @@ target_cleanup:;
static int static int
bs_read(struct burn_source *bs, unsigned char *buf, int size) bs_read(struct burn_source *bs, unsigned char *buf, int size)
{ {
ssize_t ret; int ret;
int bytes_read = 0;
Ecma119Image *t = (Ecma119Image*)bs->data; Ecma119Image *t = (Ecma119Image*)bs->data;
/* make safe against partial buffer returns */ ret = iso_ring_buffer_read(t->buffer, buf, size);
while (bytes_read < size) { if (ret == ISO_SUCCESS) {
ret = read(t->rdfd, buf + bytes_read, size - bytes_read); return size;
if (ret == 0) { } else if (ret < 0) {
/* EOF */ /* error */
return 0; iso_msg_fatal(t->image, LIBISO_READ_ERROR, "Error reading pipe");
} else if (ret < 0) { return -1;
/* error */ } else {
iso_msg_fatal(t->image, LIBISO_READ_ERROR, "Error reading pipe"); /* EOF */
return -1; return 0;
}
bytes_read += ret;
} }
return bytes_read;
} }
static off_t static off_t
@ -764,14 +758,16 @@ bs_free_data(struct burn_source *bs)
{ {
Ecma119Image *target = (Ecma119Image*)bs->data; Ecma119Image *target = (Ecma119Image*)bs->data;
/* TODO ugly, forces a SIG_PIPE if writing not finished, /* forces writer to stop if it is still running */
* but I will replace the pipe anyway, so... */ iso_ring_buffer_reader_close(target->buffer);
close(target->rdfd);
/* wait until writer thread finishes */ /* wait until writer thread finishes */
pthread_join(target->wthread, NULL); pthread_join(target->wthread, NULL);
iso_msg_debug(target->image, "Writer thread joined"); iso_msg_debug(target->image, "Writer thread joined");
iso_msg_debug(target->image, "Ring buffer was %d times full and %d times "
"empty", iso_ring_buffer_get_times_full(target->buffer),
iso_ring_buffer_get_times_empty(target->buffer));
/* now we can safety free target */ /* now we can safety free target */
ecma119_image_free(target); ecma119_image_free(target);
@ -826,13 +822,12 @@ int iso_image_create(IsoImage *image, Ecma119WriteOpts *opts,
int iso_write(Ecma119Image *target, void *buf, size_t count) int iso_write(Ecma119Image *target, void *buf, size_t count)
{ {
ssize_t result; int ret;
result = write(target->wrfd, buf, count); ret = iso_ring_buffer_write(target->buffer, buf, count);
if (result != count) { if (ret == 0) {
/* treat this as an error? */ /* reader cancelled */
return ISO_WRITE_ERROR; return ISO_WRITE_ERROR;
} else {
return ISO_SUCCESS;
} }
return ret;
} }

View File

@ -11,6 +11,7 @@
#include "libisofs.h" #include "libisofs.h"
#include "util.h" #include "util.h"
#include "buffer.h"
#include <stdint.h> #include <stdint.h>
#include <pthread.h> #include <pthread.h>
@ -79,9 +80,8 @@ struct ecma119_image {
/* tree of files sources */ /* tree of files sources */
IsoRBTree *files; IsoRBTree *files;
/* file descriptors for read and writing image */ /* Buffer for communication between burn_source and writer thread */
int rdfd; /* read from here */ IsoRingBuffer *buffer;
int wrfd; /* write to here */
/* writer thread descriptor */ /* writer thread descriptor */
pthread_t wthread; pthread_t wthread;