Commit 13839c73 authored by Roland Haas's avatar Roland Haas
Browse files

async_writer: add finalize() member to flush queue

parent 16402f78
......@@ -36,23 +36,7 @@ async_writer::async_writer(FILE* out_fh, const size_t max_bytes_queued_) :
async_writer::~async_writer()
{
// make writer thread exit
cmd_block_t exit_cmd;
exit_cmd.exit_block.cmd = CMD_EXIT;
pthread_mutex_lock(&queue_lock);
cmd_queue.push(exit_cmd);
pthread_cond_signal(&queue_wait);
pthread_mutex_unlock(&queue_lock);
// this waits for writer thread to finish
int join_ierr = pthread_join(writer_thread, NULL);
if(join_ierr != 0) {
fprintf(stderr, "Failed to join with writer thread: %d\n", join_ierr);
exit(1);
}
finalize();
}
void async_writer::write(const void* buf, size_t count)
......@@ -89,6 +73,27 @@ void async_writer::seek(long offset)
pthread_mutex_unlock(&queue_lock);
}
void async_writer::finalize()
{
// make writer thread exit
cmd_block_t exit_cmd;
exit_cmd.exit_block.cmd = CMD_EXIT;
pthread_mutex_lock(&queue_lock);
cmd_queue.push(exit_cmd);
pthread_cond_signal(&queue_wait);
pthread_mutex_unlock(&queue_lock);
// this waits for writer thread to finish
int join_ierr = pthread_join(writer_thread, NULL);
if(join_ierr != 0) {
fprintf(stderr, "Failed to join with writer thread: %d\n", join_ierr);
exit(1);
}
}
void* async_writer::writer_func(void* calldata)
{
async_writer* obj = static_cast<async_writer*>(calldata);
......
......@@ -23,7 +23,10 @@ class async_writer
// write buf to file, free() memory once done ie. buf must be obtained from
// malloc()
void write(const void* buf, size_t count);
// seek to location offset in the file
void seek(long offset);
// flush command queue and wait for writer to finish
void finalize();
private:
// data types to keep track of what the writer end needs to do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment