// the single threaded version of this call. will go away when we do direct // event delivery (soon) static VALUE method_zkrb_get_next_event_st(VALUE self) { volatile VALUE rval = Qnil; if (is_closed(self)) { zkrb_debug("we are closed, not gonna try to get an event"); return Qnil; } FETCH_DATA_PTR(self, zk); zkrb_event_t *event = zkrb_dequeue(zk->queue, 0); if (event != NULL) { rval = zkrb_event_to_ruby(event); zkrb_event_free(event); #if THREADED int fd = zk->queue->pipe_read; // we don't care in this case. this is just until i can remove the self // pipe from the queue char b[128]; while(read(fd, b, sizeof(b)) == sizeof(b)){} #endif } return rval; }
void zkrb_queue_free(zkrb_queue_t *queue) { if (queue == NULL) return; zkrb_event_t *elt = NULL; while ((elt = zkrb_dequeue(queue)) != NULL) { zkrb_event_free(elt); } free(queue); }
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; }
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; } }
static VALUE method_get_next_event(VALUE self) { char buf[64]; zkrb_event_t *event ; int fd; fd_set rset; VALUE hash ; 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_running(self)) { /* fprintf(stderr, "method_get_next_event: running is false, returning nil\n");*/ return Qnil; // this case for shutdown } event = zkrb_dequeue(zk->queue, 1); /* Wait for an event using rb_thread_select() on the queue's pipe */ if (event == NULL) { fd = zk->queue->pipe_read; FD_ZERO(&rset); FD_SET(fd, &rset); if (rb_thread_select(fd + 1, &rset, NULL, NULL, NULL) == -1) rb_raise(rb_eRuntimeError, "select failed: %d", errno); if (read(fd, buf, sizeof(buf)) == -1) rb_raise(rb_eRuntimeError, "read failed: %d", errno); continue; } hash = zkrb_event_to_ruby(event); zkrb_event_free(event); return hash; } }