Module: Iodine::Scheduler

Defined in:
ext/iodine/scheduler.c

Class Method Summary collapse

Class Method Details

.attach(r_fd, r_waittype, r_timeout) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
# File 'ext/iodine/scheduler.c', line 70

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);

  Check_Type(r_timeout, T_FIXNUM);
  size_t 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 (timeout) {
    fio_timeout_set(uuid, timeout);
  }

  fio_watch(uuid, (fio_protocol_s *)protocol);

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

.closeObject



172
173
174
175
176
177
178
# File 'ext/iodine/scheduler.c', line 172

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'ext/iodine/scheduler.c', line 147

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/iodine/scheduler.c', line 125

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;
}