Module: Iodine::Scheduler

Defined in:
ext/iodine/scheduler.c

Class Method Summary collapse

Class Method Details

.attach(r_fd, r_waittype, r_timeout) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'ext/iodine/scheduler.c', line 90

static VALUE iodine_scheduler_attach(VALUE self, VALUE r_fd, VALUE r_waittype, VALUE r_timeout) {
  Check_Type(r_fd, T_FIXNUM);
  int fd = FIX2INT(r_fd);

  Check_Type(r_waittype, T_FIXNUM);
  size_t waittype = FIX2UINT(r_waittype);

  size_t timeout;
  if (r_timeout != Qnil) {
    Check_Type(r_timeout, T_FIXNUM);
    timeout = FIX2UINT(r_timeout);
  }

  fio_set_non_block(fd);

  rb_need_block();
  VALUE block = IodineStore.add(rb_block_proc());

  scheduler_protocol_s *protocol = fio_malloc(sizeof(*protocol));
  FIO_ASSERT_ALLOC(protocol);

  if ((waittype & ATTACH_ON_READ_READY_CALLBACK) && (waittype & ATTACH_ON_WRITE_READY_CALLBACK)) {
    *protocol = (scheduler_protocol_s){
        .p.on_data = iodine_scheduler_task_perform,
        .p.on_ready = iodine_scheduler_task_perform,
        .p.on_close = iodine_scheduler_task_close,
        .p.ping = iodine_scheduler_task_timeout,
        .block = block,
    };
  } else if (waittype & ATTACH_ON_READ_READY_CALLBACK) {
    *protocol = (scheduler_protocol_s){
        .p.on_data = iodine_scheduler_task_perform,
        .p.on_ready = noop,
        .p.on_close = iodine_scheduler_task_close,
        .p.ping = iodine_scheduler_task_timeout,
        .block = block,
    };
  } else if (waittype & ATTACH_ON_WRITE_READY_CALLBACK) {
    *protocol = (scheduler_protocol_s){
        .p.on_data = noop,
        .p.on_ready = iodine_scheduler_task_perform,
        .p.on_close = iodine_scheduler_task_close,
        .p.ping = iodine_scheduler_task_timeout,
        .block = block,
    };
  }

  intptr_t uuid = fio_fd2uuid(fd);

  if (r_timeout == Qnil) {
    fio_timeout_set(uuid, 0);
  } else if (timeout) {
    fio_timeout_set(uuid, timeout);
  } else {
    // timeout was explicitly set to 0 - return right away
    protocol->p.on_ready = iodine_scheduler_task_deferred_not_ready;
  }

  fio_watch(uuid, (fio_protocol_s *)protocol);

  return LONG2NUM(uuid);
  (void)self;
}

.closeObject



201
202
203
204
205
206
207
# File 'ext/iodine/scheduler.c', line 201

static VALUE iodine_scheduler_close(VALUE self) {
  fio_defer_perform();
  while (fio_flush_all()) {}

  return Qtrue;
  (void)self;
}

.read(r_fd, r_length, r_offset) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'ext/iodine/scheduler.c', line 176

static VALUE iodine_scheduler_read(VALUE self, VALUE r_fd, VALUE r_length, VALUE r_offset) {
  Check_Type(r_fd, T_FIXNUM);
  int fd = FIX2INT(r_fd);

  Check_Type(r_length, T_FIXNUM);
  int length = FIX2INT(r_length);

  if (length == 0) {
    length = IO_MAX_READ;
  }

  intptr_t uuid = fio_fd2uuid(fd);
  char buffer[length];

  ssize_t len = fio_read_unsafe(uuid, &buffer, length);
  if (len == -1) {
    return Qnil;
  }

  return rb_str_new(buffer, len);

  (void)self;
  (void)r_offset;
}

.write(r_fd, r_buffer, r_length, r_offset) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'ext/iodine/scheduler.c', line 154

static VALUE iodine_scheduler_write(VALUE self, VALUE r_fd, VALUE r_buffer, VALUE r_length, VALUE r_offset) {
  Check_Type(r_fd, T_FIXNUM);
  int fd = FIX2INT(r_fd);

  Check_Type(r_buffer, T_STRING);
  char *buffer = RSTRING_PTR(r_buffer);

  Check_Type(r_length, T_FIXNUM);
  int length = FIX2INT(r_length);

  Check_Type(r_offset, T_FIXNUM);
  int offset = FIX2INT(r_offset);

  void *cpy = fio_malloc(length);
  memcpy(cpy, buffer, length);
  fio_write2(fio_fd2uuid(fd), .data.buffer = cpy, .length = length, .offset = offset, .after.dealloc = fio_free);

  return r_length;

  (void)self;
}