FreeBSD kqueue filter only sometimes unblocks the waiting client

时间:2016-04-04 13:57:50

标签: freebsd

I am writing kqueue hooks for a character device that allows a client to block waiting for an EVFILT_READ. If I set my read filters code to always return one the kevent will return instantly. However, if the filter returns one at some later point in time nothing unblocks. For the following code the printf "After" never happens and in the filter code I can trivially get "filter_Read return 1" (immediately followed by a return 0)

Device (relevant excerpt)

static int
lowmem_filter_read(struct knote *kn, long hint)
{
  mtx_assert(&lowmem_mtx, MA_OWNED);
  if(manual_alert){
    manual_alert=0;
    printf("filter_Read return 1\n");
    return 1;
  }
  printf("filter_Read return 0\n");
  return 0;
}

static void
lowmem_filter_detach(struct knote *kn)
{
  mtx_assert(&lowmem_mtx, MA_OWNED);
  knlist_remove(&kl, kn, 0);
}

static struct filterops lowmem_filtops_read = {
  .f_isfd = 1,
  .f_detach = lowmem_filter_detach,
  .f_event = lowmem_filter_read,
};


static int
lowmem_kqfilter(struct cdev *dev, struct knote *kn)
{
  int err = EINVAL;

  /* Figure out who needs service */
  lowmem_lock();
  switch (kn->kn_filter) {
  case EVFILT_READ:
      kn->kn_fop = &lowmem_filtops_read;
      knlist_add(&kl, kn, 1);
      err = 0;
    break;
  default:
    err = EOPNOTSUPP;
    break;
  }
  lowmem_unlock();
  return (err);
}

Client:

struct kevent ev;
struct timespec nullts = {0,0};
int fd=0;
int main(int argc, char **argv){
    fd = open("/dev/lowmem", O_RDWR | O_NONBLOCK);
    int kq=kqueue();
    EV_SET(&ev,fd,EVFILT_READ, EV_ADD,0,0,NULL);
    kevent(kq,&ev,1,NULL,0,&nullts);
    for(;;){
        printf("Starting\n");
        int n=kevent(kq,NULL,0,&ev,1,NULL);
        printf("After\n");
        if(n>0){
            printf("Something happened ev.fflags=%i\n",(int)ev.fflags);
        }
    }
    return 0;
}

0 个答案:

没有答案