Skip to content

Commit

Permalink
add idle.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Apr 6, 2012
1 parent ccb8d80 commit 0b74352
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ext/UV/eg/07_idle.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use UV;

my $tick = 0;

my $idle = UV::Idle.new;
$idle.start(sub() {
if $tick++ % 5000 == 0 {
print(".");
}
});

my $timer = UV::Timer.new;
$timer.start(sub() {
print("|");
}, 1000, 1000);

UV.run();
66 changes: 66 additions & 0 deletions ext/UV/uv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct _uv_data {
SharedPtr<CodeValue> read_cb;
SharedPtr<CodeValue> close_cb;
SharedPtr<CodeValue> timer_cb;
SharedPtr<CodeValue> idle_cb;
_uv_data(VM* vm, uv_loop_t* loop, void* data) {
this->vm = vm;
this->loop = loop;
Expand Down Expand Up @@ -344,6 +345,63 @@ static SharedPtr<Value> _uv_accept(VM * vm, Value* self) {
}
}

static SharedPtr<Value> _uv_idle_new(VM * vm, const std::vector<SharedPtr<Value>>& args) {
SharedPtr<Value> self = args.at(0);
uv_loop_t* loop;
if (args.size() == 1) {
loop = uv_default_loop();
} else {
loop = static_cast<uv_loop_t*>(args.at(2)->upcast<ObjectValue>()->data()->upcast<PointerValue>()->ptr());
}
uv_idle_t* idle = new uv_idle_t;
if (uv_idle_init(loop, idle) != 0) {
delete idle;
throw new ExceptionValue(uv_strerror(uv_last_error(loop)));
} else {
idle->data = new _uv_data(vm, loop, NULL);
return new ObjectValue(vm, vm->symbol_table->get_id("UV::Idle"), new PointerValue(idle));
}
}

void __uv_idle_cb(uv_idle_t* idle, int status) {
VM *vm = ((_uv_data*) idle->data)->vm;
SharedPtr<CodeValue> callback = ((_uv_data*) idle->data)->idle_cb;
if (callback != NULL) {
vm->stack.push_back(new IntValue(status));
vm->function_call_ex(1, callback, UndefValue::instance());
}
}

static SharedPtr<Value> _uv_idle_start(VM * vm, Value* self, Value* callback_v) {
assert(self->value_type == VALUE_TYPE_OBJECT);
SharedPtr<Value> idle_ = self->upcast<ObjectValue>()->data();
assert(idle_->value_type == VALUE_TYPE_POINTER);
uv_idle_t *idle = (uv_idle_t*) idle_->upcast<PointerValue>()->ptr();

if (callback_v->value_type != VALUE_TYPE_CODE) {
((_uv_data*) idle->data)->idle_cb = NULL;
} else {
((_uv_data*) idle->data)->idle_cb = callback_v->upcast<CodeValue>();
}

if (uv_idle_start(idle, __uv_idle_cb) != 0) {
throw new ExceptionValue(uv_strerror(uv_last_error(((_uv_data*) idle->data)->loop)));
}
return UndefValue::instance();
}

static SharedPtr<Value> _uv_idle_stop(VM * vm, Value* self) {
assert(self->value_type == VALUE_TYPE_OBJECT);
SharedPtr<Value> idle_ = self->upcast<ObjectValue>()->data();
assert(idle_->value_type == VALUE_TYPE_POINTER);
uv_idle_t *idle = (uv_idle_t*) idle_->upcast<PointerValue>()->ptr();

if (uv_idle_stop(idle) != 0) {
throw new ExceptionValue(uv_strerror(uv_last_error(((_uv_data*) idle->data)->loop)));
}
return UndefValue::instance();
}

extern "C" {

TORA_EXPORT
Expand Down Expand Up @@ -381,6 +439,14 @@ void Init_UV_(VM* vm) {
pkg->add_method(vm->symbol_table->get_id("accept"), new CallbackFunction(_uv_accept));
pkg->add_method(vm->symbol_table->get_id("DESTROY"), new CallbackFunction(_uv_DESTROY));
}
{
SharedPtr<Package> pkg = vm->find_package("UV::Idle");
pkg->add_method(vm->symbol_table->get_id("new"), new CallbackFunction(_uv_idle_new));
pkg->add_method(vm->symbol_table->get_id("start"), new CallbackFunction(_uv_idle_start));
pkg->add_method(vm->symbol_table->get_id("stop"), new CallbackFunction(_uv_idle_stop));
pkg->add_method(vm->symbol_table->get_id("close"), new CallbackFunction(_uv_close));
pkg->add_method(vm->symbol_table->get_id("DESTROY"), new CallbackFunction(_uv_DESTROY));
}
}

}

0 comments on commit 0b74352

Please sign in to comment.