关键词搜索

源码搜索 ×
×

漫话Redis源码之一百一二十

发布2022-06-19浏览382次

详情内容

掩码的使用,还真得会:

  1. /*
  2. * Helper function to invoke port_associate for the given fd and mask.
  3. */
  4. static int aeApiAssociate(const char *where, int portfd, int fd, int mask) {
  5. int events = 0;
  6. int rv, err;
  7. if (mask & AE_READABLE)
  8. events |= POLLIN;
  9. if (mask & AE_WRITABLE)
  10. events |= POLLOUT;
  11. if (evport_debug)
  12. fprintf(stderr, "%s: port_associate(%d, 0x%x) = ", where, fd, events);
  13. rv = port_associate(portfd, PORT_SOURCE_FD, fd, events,
  14. (void *)(uintptr_t)mask);
  15. err = errno;
  16. if (evport_debug)
  17. fprintf(stderr, "%d (%s)\n", rv, rv == 0 ? "no error" : strerror(err));
  18. if (rv == -1) {
  19. fprintf(stderr, "%s: port_associate: %s\n", where, strerror(err));
  20. if (err == EAGAIN)
  21. fprintf(stderr, "aeApiAssociate: event port limit exceeded.");
  22. }
  23. return rv;
  24. }
  25. static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
  26. aeApiState *state = eventLoop->apidata;
  27. int fullmask, pfd;
  28. if (evport_debug)
  29. fprintf(stderr, "aeApiAddEvent: fd %d mask 0x%x\n", fd, mask);
  30. /*
  31. * Since port_associate's "events" argument replaces any existing events, we
  32. * must be sure to include whatever events are already associated when
  33. * we call port_associate() again.
  34. */
  35. fullmask = mask | eventLoop->events[fd].mask;
  36. pfd = aeApiLookupPending(state, fd);
  37. if (pfd != -1) {
  38. /*
  39. * This fd was recently returned from aeApiPoll. It should be safe to
  40. * assume that the consumer has processed that poll event, but we play
  41. * it safer by simply updating pending_mask. The fd will be
  42. * re-associated as usual when aeApiPoll is called again.
  43. */
  44. if (evport_debug)
  45. fprintf(stderr, "aeApiAddEvent: adding to pending fd %d\n", fd);
  46. state->pending_masks[pfd] |= fullmask;
  47. return 0;
  48. }
  49. return (aeApiAssociate("aeApiAddEvent", state->portfd, fd, fullmask));
  50. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载