Class: Event::Backend::KQueue

Inherits:
Object
  • Object
show all
Defined in:
ext/event/backend/kqueue.c

Instance Method Summary collapse

Constructor Details

#initialize(loop) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'ext/event/backend/kqueue.c', line 82

VALUE Event_Backend_KQueue_initialize(VALUE self, VALUE loop) {
	struct Event_Backend_KQueue *data = NULL;
	TypedData_Get_Struct(self, struct Event_Backend_KQueue, &Event_Backend_KQueue_Type, data);
	
	data->loop = loop;
	int result = kqueue();
	
	if (result == -1) {
		rb_sys_fail("kqueue");
	} else {
		ioctl(result, FIOCLEX);
		data->descriptor = result;
		
		rb_update_max_fd(data->descriptor);
	}
	
	return self;
}

Instance Method Details

#io_wait(fiber, io, events) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'ext/event/backend/kqueue.c', line 101

VALUE Event_Backend_KQueue_io_wait(VALUE self, VALUE fiber, VALUE io, VALUE events) {
	struct Event_Backend_KQueue *data = NULL;
	TypedData_Get_Struct(self, struct Event_Backend_KQueue, &Event_Backend_KQueue_Type, data);
	
	struct kevent event;
	u_short flags = 0;
	
	int descriptor = NUM2INT(rb_funcall(io, id_fileno, 0));
	
	int mask = NUM2INT(events);
	
	if (mask & READABLE) {
		flags |= EVFILT_READ;
	}
	
	if (mask & PRIORITY) {
		flags |= EV_OOBAND;
	}
	
	if (mask & WRITABLE) {
		flags |= EVFILT_WRITE;
	}

	EV_SET(&event, descriptor, flags, EV_ADD|EV_ENABLE|EV_ONESHOT, 0, 0, (void*)fiber);
	
	// A better approach is to batch all changes:
	int result = kevent(data->descriptor, &event, 1, NULL, 0, NULL);
	
	if (result == -1) {
		rb_sys_fail("kevent");
	}
	
	rb_funcall(data->loop, id_transfer, 0);
	
	return Qnil;
}

#select(duration) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'ext/event/backend/kqueue.c', line 164

VALUE Event_Backend_KQueue_select(VALUE self, VALUE duration) {
	struct Event_Backend_KQueue *data = NULL;
	TypedData_Get_Struct(self, struct Event_Backend_KQueue, &Event_Backend_KQueue_Type, data);
	
	struct kevent events[KQUEUE_MAX_EVENTS];
	struct timespec storage;
	
	int count = kevent(data->descriptor, NULL, 0, events, KQUEUE_MAX_EVENTS, make_timeout(duration, &storage));
	
	if (count == -1) {
		rb_sys_fail("kevent");
	}
	
	for (int i = 0; i < count; i += 1) {
		VALUE fiber = (VALUE)events[i].udata;
		rb_funcall(fiber, id_transfer, 0);
	}
	
	return INT2NUM(count);
}