Exemplo n.º 1
0
// wake up the event loop, used when shutting down
static VALUE method_wake_event_loop_bang(VALUE self) {
  FETCH_DATA_PTR(self, zk); 

  zkrb_debug_inst(self, "Waking event loop: %p", zk->queue);
  zkrb_signal(zk->queue);

  return Qnil;
};
Exemplo n.º 2
0
static VALUE method_get_next_event(VALUE self, VALUE blocking) {
  char buf[64];
  FETCH_DATA_PTR(self, zk);

  for (;;) {

    // we use the is_running(self) method here because it allows us to have a
    // ruby-land semaphore that we can also use in the java extension
    //
    if (is_closed(self) || !is_running(self)) {
      zkrb_debug_inst(self, "is_closed(self): %d, is_running(self): %d, method_get_next_event is exiting loop", is_closed(self), is_running(self));
      return Qnil;  // this case for shutdown
    }

    zkrb_event_t *event = zkrb_dequeue(zk->queue, 1);

    /* Wait for an event using rb_thread_select() on the queue's pipe */
    if (event == NULL) {
      if (NIL_P(blocking) || (blocking == Qfalse)) { 
        return Qnil; // no event for us
      } 
      else {
        int fd = zk->queue->pipe_read;
        ssize_t bytes_read = 0;

        // wait for an fd to become readable, opposite of rb_thread_fd_writable
        rb_thread_wait_fd(fd);

        bytes_read = read(fd, buf, sizeof(buf));

        if (bytes_read == -1) {
          rb_raise(rb_eRuntimeError, "read failed: %d", errno);
        }

        zkrb_debug_inst(self, "read %zd bytes from the queue (%p)'s pipe", bytes_read, zk->queue);

        continue;
      }
    }

    VALUE hash = zkrb_event_to_ruby(event);
    zkrb_event_free(event);
    return hash;
  }
}
Exemplo n.º 3
0
static VALUE method_close_handle(VALUE self) {
  FETCH_DATA_PTR(self, zk);

  if (ZKRBDebugging) {
/*    fprintf(stderr, "CLOSING ZK INSTANCE: obj_id %lx", FIX2LONG(rb_obj_id(self)));*/
    zkrb_debug_inst(self, "CLOSING_ZK_INSTANCE");
    print_zkrb_instance_data(zk);
  }
  
  // this is a value on the ruby side we can check to see if destroy_zkrb_instance
  // has been called
  rb_iv_set(self, "@_closed", Qtrue);

  zkrb_debug_inst(self, "calling destroy_zkrb_instance");

  /* Note that after zookeeper_close() returns, ZK handle is invalid */
  int rc = destroy_zkrb_instance(zk);

  zkrb_debug("destroy_zkrb_instance returned: %d", rc);

  return Qnil;
/*  return INT2FIX(rc);*/
}
Exemplo n.º 4
0
static VALUE method_get_next_event(VALUE self, VALUE blocking) {
  // dbg.h 
  check_debug(!is_closed(self), "we are closed, not trying to get event");

  char buf[64];
  FETCH_DATA_PTR(self, zk);

  for (;;) {
    check_debug(!is_closed(self), "we're closed in the middle of method_get_next_event, bailing");

    zkrb_event_t *event = zkrb_dequeue(zk->queue, 1);

    /* Wait for an event using rb_thread_select() on the queue's pipe */
    if (event == NULL) {
      if (NIL_P(blocking) || (blocking == Qfalse)) { 
        goto error;
      } 
      else {
        // if we're shutting down, don't enter this section, we don't want to block
        check_debug(!is_shutting_down(self), "method_get_next_event, we're shutting down, don't enter blocking section");

        int fd = zk->queue->pipe_read;
        ssize_t bytes_read = 0;

        // wait for an fd to become readable, opposite of rb_thread_fd_writable
        rb_thread_wait_fd(fd);

        // clear all bytes here, we'll catch all the events on subsequent calls
        // (until we run out of events)
        bytes_read = read(fd, buf, sizeof(buf));

        if (bytes_read == -1) {
          rb_raise(rb_eRuntimeError, "read failed: %d", errno);
        }

        zkrb_debug_inst(self, "read %zd bytes from the queue (%p)'s pipe", bytes_read, zk->queue);

        continue;
      }
    }

    VALUE hash = zkrb_event_to_ruby(event);
    zkrb_event_free(event);
    return hash;
  }

  error: 
    return Qnil;
}