ext/rbczmq/loop.c in rbczmq-1.7.2 vs ext/rbczmq/loop.c in rbczmq-1.7.3

- old
+ new

@@ -209,10 +209,23 @@ xfree(loop); } } /* + * :nodoc: + * GC mark callback + * +*/ +static void rb_czmq_mark_loop(void *ptr) +{ + zmq_loop_wrapper *loop = (zmq_loop_wrapper *)ptr; + if (loop) { + rb_gc_mark(loop->items); + } +} + +/* * call-seq: * ZMQ::Loop.new => ZMQ::Loop * * Creates a new reactor instance. Several loops per process is supported for the lower level API. * @@ -223,16 +236,17 @@ static VALUE rb_czmq_loop_new(VALUE loop) { zmq_loop_wrapper *lp = NULL; errno = 0; - loop = Data_Make_Struct(rb_cZmqLoop, zmq_loop_wrapper, 0, rb_czmq_free_loop_gc, lp); + loop = Data_Make_Struct(rb_cZmqLoop, zmq_loop_wrapper, rb_czmq_mark_loop, rb_czmq_free_loop_gc, lp); lp->loop = zloop_new(); ZmqAssertObjOnAlloc(lp->loop, lp); lp->flags = 0; lp->running = false; lp->verbose = false; + lp->items = rb_ary_new(); rb_obj_call_init(loop, 0, NULL); return loop; } /* @@ -392,26 +406,27 @@ * Registers a poll item with the reactor. Only ZMQ::POLLIN and ZMQ::POLLOUT events are supported. * * === Examples * loop = ZMQ::Loop.new => ZMQ::Loop * item = ZMQ::Pollitem.new(sock, ZMQ::POLLIN) => ZMQ::Pollitem - * loop.register(item) => true + * loop.register(item) => ZMQ::Pollitem (coerced) * */ static VALUE rb_czmq_loop_register(VALUE obj, VALUE pollable) { int rc; errno = 0; ZmqGetLoop(obj); pollable = rb_czmq_pollitem_coerce(pollable); ZmqGetPollitem(pollable); + rb_ary_push(loop->items, pollable); rc = zloop_poller(loop->loop, pollitem->item, rb_czmq_loop_pollitem_callback, (void *)pollitem); ZmqAssert(rc); /* Let pollable be verbose if loop is verbose */ if (loop->verbose == true) rb_czmq_pollitem_set_verbose(pollable, Qtrue); - return Qtrue; + return pollable; } /* * call-seq: * loop.remove(item) => nil @@ -431,10 +446,11 @@ errno = 0; ZmqGetLoop(obj); pollable = rb_czmq_pollitem_coerce(pollable); ZmqGetPollitem(pollable); zloop_poller_end(loop->loop, pollitem->item); + rb_ary_delete(loop->items, pollable); return Qnil; } /* * call-seq: @@ -454,10 +470,11 @@ int rc; errno = 0; ZmqGetLoop(obj); ZmqGetTimer(tm); rc = zloop_timer(loop->loop, timer->delay, timer->times, rb_czmq_loop_timer_callback, (void *)tm); + rb_ary_push(loop->items, tm); ZmqAssert(rc); return Qtrue; } /* @@ -479,9 +496,10 @@ int rc; errno = 0; ZmqGetLoop(obj); ZmqGetTimer(tm); rc = zloop_timer_end(loop->loop, (void *)tm); + rb_ary_delete(loop->items, tm); ZmqAssert(rc); return Qtrue; } void _init_rb_czmq_loop()