Ejemplo n.º 1
0
void*
writer_run(void *arg)
{
  writer_t *w = (writer_t*)arg;
  block_t *b = NULL;
  int32_t wait;
  uint64_t n = 0;

  //fprintf(stderr, "writer starting w->output->n=%d\n", w->output->n);
  
  while(!w->is_done) {
#ifdef PBGZF_USE_LOCAL_POOLS
      while(w->pool_local->n < w->pool_local->m) { // more to read from the output queue
          wait = (0 == w->pool_local->n) ? 1 : 0;
          b = queue_get(w->output, wait);
          if(NULL == b) {
              if(1 == wait) {
                  if(QUEUE_STATE_OK == w->output->state) {
                      fprintf(stderr, "writer queue_get: bug encountered\n");
                      exit(1);
                  }
                  else if(QUEUE_STATE_EOF == w->output->state || 0 == w->output->num_adders) {
                      break;
                  }
                  else {
                      queue_wait_until_not_flush(w->output);
                      continue;
                  }
              }
              else {
                  break;
              }
          }
          if(0 == block_pool_add(w->pool_local, b)) {
              fprintf(stderr, "writer block_pool_add: bug encountered\n");
              exit(1);
          }
          b = NULL;
      }
      //fprintf(stderr, "writer: read from output w->pool_local->n=%d %d\n", w->pool_local->n, w->pool_local->m);

      if(0 == w->pool_local->n && (QUEUE_STATE_EOF == w->output->state || 0 == w->output->num_adders)) {
          break;
      }

      while(0 < w->pool_local->n) { // write all the blocks
          b = block_pool_get(w->pool_local);
          if(NULL == b) {
              fprintf(stderr, "writer block_pool_get: bug encountered\n");
              exit(1);
          }
          if(0 == w->compress) {
              if(writer_write_block1(w->fp_file, b) != b->block_length) {
                  fprintf(stderr, "writer writer_write_block: bug encountered\n");
                  exit(1);
              }
          }
          else {
              if(writer_write_block2(w->fp_bgzf, b) != b->block_length) {
                  fprintf(stderr, "writer writer_write_block: bug encountered\n");
                  exit(1);
              }
          }
          if(0 == block_pool_add(w->pool_fp, b)) {
              fprintf(stderr, "writer block_pool_add: bug encountered\n");
              exit(1);
          }
          n++;
      }
#else
      wait = 1;
      b = queue_get(w->output, wait);
      if(NULL == b) {
          if(1 == wait) {
              if(QUEUE_STATE_OK == w->output->state) {
                  fprintf(stderr, "writer queue_get: bug encountered\n");
                  exit(1);
              }
              else if(QUEUE_STATE_EOF == w->output->state || 0 == w->output->num_adders) {
                  break;
              }
              else {
                  queue_wait_until_not_flush(w->output);
                  continue;
              }
          }
          else {
              break;
          }
      }
      if(0 == w->compress) {
          if(writer_write_block1(w->fp_file, b) != b->block_length) {
              fprintf(stderr, "writer writer_write_block: bug encountered\n");
              exit(1);
          }
      }
      else {
          if(writer_write_block2(w->fp_bgzf, b) != b->block_length) {
              fprintf(stderr, "writer writer_write_block: bug encountered\n");
              exit(1);
          }
      }
      block_destroy(b);
      b = NULL;
      n++;
#endif
  }

  w->is_done = 1;
  //fprintf(stderr, "writer written %llu blocks\n", n);

  // NB: will wake all
  queue_remove_getter(w->output);

  return arg;
}
Ejemplo n.º 2
0
void*
reader_run(void *arg)
{
  reader_t *r = (reader_t*)arg;
  block_t *b = NULL;
  int32_t wait;
  uint64_t n = 0;
  block_pool_t *pool;
  
  //fprintf(stderr, "reader staring\n");

  pool = block_pool_init2(PBGZF_BLOCKS_POOL_NUM);

  while(!r->is_done) {
#ifdef PBGZF_USE_LOCAL_POOLS
      // read block
      while(pool->n < pool->m) {
          if(NULL == r->pool || NULL == (b = block_pool_get(r->pool))) {
              b = block_init(); 
          }
          if(0 == r->compress) {
              if(reader_read_block(r->fp_bgzf, b) < 0) {
                  fprintf(stderr, "reader reader_read_block: bug encountered\n");
                  exit(1);
              }
          }
          else { 
              if((b->block_length = read(r->fd_file, b->buffer, WINDOW_SIZE)) < 0) {
                  fprintf(stderr, "reader read: bug encountered\n");
                  exit(1);
              }
          }
          if(NULL == b || 0 == b->block_length) {
              block_pool_add(r->pool, b);
              b = NULL;
              break;
          }
          if(0 == block_pool_add(pool, b)) {
              fprintf(stderr, "reader block_pool_add: bug encountered\n");
              exit(1);
          }
      }
      //fprintf(stderr, "reader: read in pool->n=%d\n", pool->n);

      if(0 == pool->n) {
          break;
      }

      // add to the queue
      while(0 < pool->n) {
          b = block_pool_peek(pool);
          if(NULL == b) {
              fprintf(stderr, "reader block_pool_get: bug encountered\n");
              exit(1);
          }
          wait = (pool->n == pool->m) ? 1 : 0; // NB: only wait if we cannot read in any more...
          if(0 == queue_add(r->input, b, wait)) {
              if(1 == wait) {
                  if(QUEUE_STATE_OK == r->input->state) {
                      fprintf(stderr, "reader queue_add: bug encountered\n");
                      exit(1);
                  }
                  else if(QUEUE_STATE_EOF == r->input->state) { // EOF, quit
                      break;
                  }
                  else {
                      // NB: if the reader has blocks, it does not make sense to
                      // flush
                      fprintf(stderr, "reader queue_add: bug encountered\n");
                      exit(1);
                  }
              }
              else {
                  break;
              }
          }
          block_pool_get(pool); // ignore return
          b = NULL;
          n++;
      }
      //fprintf(stderr, "reader: add to pool->n=%d\n", pool->n);
      //fprintf(stderr, "r->output->n=%d\n", r->input->n);
#else
      // read block
      //fprintf(stderr, "Reader #%d read block\n", 0);
      b = block_init(); 
      if(0 == r->compress) {
          if(reader_read_block(r->fp_bgzf, b) < 0) {
              fprintf(stderr, "reader reader_read_block: bug encountered\n");
              exit(1);
          }
      }
      else { 
          if((b->block_length = read(r->fd_file, b->buffer, WINDOW_SIZE)) < 0) {
              fprintf(stderr, "reader read: bug encountered\n");
              exit(1);
          }
      }
      if(NULL == b || 0 == b->block_length) {
          block_destroy(b);
          b = NULL;
          break;
      }

      // add to the queue
      //fprintf(stderr, "Reader #%d add to queue\n", 0);
      wait = 1;
      if(0 == queue_add(r->input, b, wait)) {
          if(1 == wait) {
              if(QUEUE_STATE_OK == r->input->state) {
                  fprintf(stderr, "reader queue_add: bug encountered\n");
                  exit(1);
              }
              else if(QUEUE_STATE_EOF == r->input->state) { // EOF, quit
                  block_destroy(b);
                  b = NULL;
                  break;
              }
              else {
                  queue_wait_until_not_flush(r->input);
                  continue;
              }
          }
          else {
              block_destroy(b);
              b = NULL;
              break;
          }
      }
      b = NULL;
      n++;
      //fprintf(stderr, "reader read %llu blocks\n", n);
#endif
  }
  block_destroy(b);
  b = NULL;
  
  r->is_done = 1;
  
  // NB: EOF should be handled when the adder is removed
  queue_remove_adder(r->input);
  
  //fprintf(stderr, "reader read %llu blocks\n", n);
  //fprintf(stderr, "reader r->input->n=%d\n", r->input->n);
  //fprintf(stderr, "reader r->input->state=%d QUEUE_STATE_EOF=%d\n", r->input->state, QUEUE_STATE_EOF);

  block_pool_destroy(pool);

  return arg;
}