Class: Kqueue

Inherits:
IO
  • Object
show all
Defined in:
ext/kqueue/core.c

Instance Method Summary collapse

Constructor Details

#initializeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'ext/kqueue/core.c', line 30

static VALUE
rb_kqueue_initialize(VALUE self)
{
  rb_io_t *fp;
  int fd;

  fd = kqueue();
  if (fd == -1)
    rb_sys_fail("kqueue");
  rb_update_max_fd(fd);

  MakeOpenFile(self, fp);
  fp->fd = fd;
  fp->mode = FMODE_READABLE|FMODE_BINMODE;
  rb_io_ascii8bit_binmode(self);

  IVAR_SET(self, rb_hash_new());

  return self;
}

Instance Method Details

#kevent(*args) ⇒ Object



67
68
69
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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'ext/kqueue/core.c', line 67

static VALUE
rb_kqueue_kevent(int argc, VALUE *argv, VALUE self)
{
  VALUE ch_list, max_events, timeout, ev, ready_evlist;
  struct kqueue_kevent_args args;
  struct timespec ts;
  struct timespec *pts = NULL;
  struct timeval tv;
  int i;
  int ev_len = 0, ch_len = 0;
  int ready;
  struct kevent *chl = NULL, *evl = NULL;
  rb_io_t *fptr, *fptr_io;

  fptr = RFILE(self)->fptr;
  rb_io_check_initialized(fptr);

  rb_scan_args(argc, argv, "21", &ch_list, &max_events, &timeout);
  if (!NIL_P(max_events)) {
    ev_len = FIX2INT(max_events);
  }

  if (!NIL_P(timeout)) {
    tv = rb_time_timeval(timeout);
    ts.tv_sec = (time_t)tv.tv_sec;
    ts.tv_nsec = (long)tv.tv_usec * 1000;
    pts = &ts;
  }

  if (!NIL_P(ch_list)) {
    Check_Type(ch_list, T_ARRAY);
    ch_len = RARRAY_LEN(ch_list);

    chl = alloca(sizeof(struct kevent) * ch_len);
    for (i = 0; i < ch_len; i++) {
      ev = RARRAY_AREF(ch_list, i);
      if (!rb_obj_is_kind_of(ev, cKqueue_Event)) {
        rb_raise(rb_eTypeError, "must be set Array of Event object");
      }

      chl[i].ident = FIX2INT(RSTRUCT_GET(ev, IDENT));
      chl[i].filter = FIX2INT(RSTRUCT_GET(ev, FILTER));
      chl[i].flags = FIX2UINT(RSTRUCT_GET(ev, FLAGS));
      chl[i].fflags = FIX2UINT(RSTRUCT_GET(ev, FFLAGS));
      chl[i].data = NUM2LONG(RSTRUCT_GET(ev, DATA));
      {
        VALUE fileno = RSTRUCT_GET(ev, IDENT);
        VALUE filter = RSTRUCT_GET(ev, FILTER);
        VALUE udata = RSTRUCT_GET(ev, UDATA);
        VALUE ivar = IVAR_GET(self);
        VALUE h = rb_hash_aref(ivar, fileno);
        if ((chl[i].flags & EV_DELETE) == EV_DELETE) {
          if (chl[i].flags & ~EV_DELETE) {
            rb_raise(rb_eArgError, "EV_DELETE cannot set with other");
          }
          if (NIL_P(h)) {
            rb_raise(rb_eArgError, "delete udata not found");
          }
          rb_hash_delete(h, filter);
          if (RHASH_EMPTY_P(h)) {
            rb_hash_delete(ivar, fileno);
          }
          udata = (VALUE)0;
        }
        else {
          if (NIL_P(h)) {
            h = rb_hash_aset(ivar, fileno, rb_hash_new());
          }
          rb_hash_aset(h, filter, udata);
        }
        IVAR_SET(self, ivar);
        chl[i].udata = (void*)udata;
      }
    }
  }

  if (0 < ev_len) {
    evl = alloca(sizeof(struct kevent) * ev_len);
  }
  else if (ev_len < 0) {
    rb_raise(rb_eArgError, "negative size");
  }

  args.fd = fptr->fd;
  args.chl = chl;
  args.ch_len = ch_len;
  args.evl = evl;
  args.ev_len = ev_len;
  args.ts = pts;

RETRY:
  ready = (int)(long)rb_thread_call_without_gvl(rb_kqueue_kevent_func, &args, RUBY_UBF_IO, 0);
  if (ready == -1) {
    if (errno == EINTR)
      goto RETRY;
    else
      rb_sys_fail("kevent");
  }

  ready_evlist = rb_ary_new_capa(ready);
  for (i = 0; i < ready; i++) {
    ev = rb_obj_alloc(cKqueue_Event);
    RSTRUCT_SET(ev, IDENT, INT2FIX(evl[i].ident));
    RSTRUCT_SET(ev, FILTER, INT2FIX(evl[i].filter));
    RSTRUCT_SET(ev, FLAGS, INT2FIX(evl[i].flags));
    RSTRUCT_SET(ev, FFLAGS, INT2FIX(evl[i].fflags));
    RSTRUCT_SET(ev, FLAGS, INT2FIX(evl[i].flags));
    RSTRUCT_SET(ev, DATA, INT2FIX(evl[i].data));
    RSTRUCT_SET(ev, UDATA, (VALUE)evl[i].udata);
    rb_ary_store(ready_evlist, i, ev);
  }
  return ready_evlist;
}