漫话Redis源码之八十三

2024-02-06 09:38
文章标签 源码 redis 漫话 八十三

本文主要是介绍漫话Redis源码之八十三,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

触发并掩码掉给定的fd:

static int aeApiLookupPending(aeApiState *state, int fd) {uint_t i;for (i = 0; i < state->npending; i++) {if (state->pending_fds[i] == fd)return (i);}return (-1);
}/** Helper function to invoke port_associate for the given fd and mask.*/
static int aeApiAssociate(const char *where, int portfd, int fd, int mask) {int events = 0;int rv, err;if (mask & AE_READABLE)events |= POLLIN;if (mask & AE_WRITABLE)events |= POLLOUT;if (evport_debug)fprintf(stderr, "%s: port_associate(%d, 0x%x) = ", where, fd, events);rv = port_associate(portfd, PORT_SOURCE_FD, fd, events,(void *)(uintptr_t)mask);err = errno;if (evport_debug)fprintf(stderr, "%d (%s)\n", rv, rv == 0 ? "no error" : strerror(err));if (rv == -1) {fprintf(stderr, "%s: port_associate: %s\n", where, strerror(err));if (err == EAGAIN)fprintf(stderr, "aeApiAssociate: event port limit exceeded.");}return rv;
}static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {aeApiState *state = eventLoop->apidata;int fullmask, pfd;if (evport_debug)fprintf(stderr, "aeApiAddEvent: fd %d mask 0x%x\n", fd, mask);/** Since port_associate's "events" argument replaces any existing events, we* must be sure to include whatever events are already associated when* we call port_associate() again.*/fullmask = mask | eventLoop->events[fd].mask;pfd = aeApiLookupPending(state, fd);if (pfd != -1) {/** This fd was recently returned from aeApiPoll.  It should be safe to* assume that the consumer has processed that poll event, but we play* it safer by simply updating pending_mask.  The fd will be* re-associated as usual when aeApiPoll is called again.*/if (evport_debug)fprintf(stderr, "aeApiAddEvent: adding to pending fd %d\n", fd);state->pending_masks[pfd] |= fullmask;return 0;}return (aeApiAssociate("aeApiAddEvent", state->portfd, fd, fullmask));
}static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {aeApiState *state = eventLoop->apidata;int fullmask, pfd;if (evport_debug)fprintf(stderr, "del fd %d mask 0x%x\n", fd, mask);pfd = aeApiLookupPending(state, fd);if (pfd != -1) {if (evport_debug)fprintf(stderr, "deleting event from pending fd %d\n", fd);/** This fd was just returned from aeApiPoll, so it's not currently* associated with the port.  All we need to do is update* pending_mask appropriately.*/state->pending_masks[pfd] &= ~mask;if (state->pending_masks[pfd] == AE_NONE)state->pending_fds[pfd] = -1;return;}/** The fd is currently associated with the port.  Like with the add case* above, we must look at the full mask for the file descriptor before* updating that association.  We don't have a good way of knowing what the* events are without looking into the eventLoop state directly.  We rely on* the fact that our caller has already updated the mask in the eventLoop.*/fullmask = eventLoop->events[fd].mask;if (fullmask == AE_NONE) {/** We're removing *all* events, so use port_dissociate to remove the* association completely.  Failure here indicates a bug.*/if (evport_debug)fprintf(stderr, "aeApiDelEvent: port_dissociate(%d)\n", fd);if (port_dissociate(state->portfd, PORT_SOURCE_FD, fd) != 0) {perror("aeApiDelEvent: port_dissociate");abort(); /* will not return */}} else if (aeApiAssociate("aeApiDelEvent", state->portfd, fd,fullmask) != 0) {/** ENOMEM is a potentially transient condition, but the kernel won't* generally return it unless things are really bad.  EAGAIN indicates* we've reached a resource limit, for which it doesn't make sense to* retry (counter-intuitively).  All other errors indicate a bug.  In any* of these cases, the best we can do is to abort.*/abort(); /* will not return */}
}static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {aeApiState *state = eventLoop->apidata;struct timespec timeout, *tsp;uint_t mask, i;uint_t nevents;port_event_t event[MAX_EVENT_BATCHSZ];/** If we've returned fd events before, we must re-associate them with the* port now, before calling port_get().  See the block comment at the top of* this file for an explanation of why.*/for (i = 0; i < state->npending; i++) {if (state->pending_fds[i] == -1)/* This fd has since been deleted. */continue;if (aeApiAssociate("aeApiPoll", state->portfd,state->pending_fds[i], state->pending_masks[i]) != 0) {/* See aeApiDelEvent for why this case is fatal. */abort();}state->pending_masks[i] = AE_NONE;state->pending_fds[i] = -1;}state->npending = 0;if (tvp != NULL) {timeout.tv_sec = tvp->tv_sec;timeout.tv_nsec = tvp->tv_usec * 1000;tsp = &timeout;} else {tsp = NULL;}/** port_getn can return with errno == ETIME having returned some events (!).* So if we get ETIME, we check nevents, too.*/nevents = 1;if (port_getn(state->portfd, event, MAX_EVENT_BATCHSZ, &nevents,tsp) == -1 && (errno != ETIME || nevents == 0)) {if (errno == ETIME || errno == EINTR)return 0;/* Any other error indicates a bug. */perror("aeApiPoll: port_get");abort();}state->npending = nevents;for (i = 0; i < nevents; i++) {mask = 0;if (event[i].portev_events & POLLIN)mask |= AE_READABLE;if (event[i].portev_events & POLLOUT)mask |= AE_WRITABLE;eventLoop->fired[i].fd = event[i].portev_object;eventLoop->fired[i].mask = mask;if (evport_debug)fprintf(stderr, "aeApiPoll: fd %d mask 0x%x\n",(int)event[i].portev_object, mask);state->pending_fds[i] = event[i].portev_object;state->pending_masks[i] = (uintptr_t)event[i].portev_user;}return nevents;
}static char *aeApiName(void) {return "evport";
}

这篇关于漫话Redis源码之八十三的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/683881

相关文章

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Redis中Hash从使用过程到原理说明

《Redis中Hash从使用过程到原理说明》RedisHash结构用于存储字段-值对,适合对象数据,支持HSET、HGET等命令,采用ziplist或hashtable编码,通过渐进式rehash优化... 目录一、开篇:Hash就像超市的货架二、Hash的基本使用1. 常用命令示例2. Java操作示例三

Redis中Set结构使用过程与原理说明

《Redis中Set结构使用过程与原理说明》本文解析了RedisSet数据结构,涵盖其基本操作(如添加、查找)、集合运算(交并差)、底层实现(intset与hashtable自动切换机制)、典型应用场... 目录开篇:从购物车到Redis Set一、Redis Set的基本操作1.1 编程常用命令1.2 集

Redis中的有序集合zset从使用到原理分析

《Redis中的有序集合zset从使用到原理分析》Redis有序集合(zset)是字符串与分值的有序映射,通过跳跃表和哈希表结合实现高效有序性管理,适用于排行榜、延迟队列等场景,其时间复杂度低,内存占... 目录开篇:排行榜背后的秘密一、zset的基本使用1.1 常用命令1.2 Java客户端示例二、zse

Redis中的AOF原理及分析

《Redis中的AOF原理及分析》Redis的AOF通过记录所有写操作命令实现持久化,支持always/everysec/no三种同步策略,重写机制优化文件体积,与RDB结合可平衡数据安全与恢复效率... 目录开篇:从日记本到AOF一、AOF的基本执行流程1. 命令执行与记录2. AOF重写机制二、AOF的

Redis高性能Key-Value存储与缓存利器常见解决方案

《Redis高性能Key-Value存储与缓存利器常见解决方案》Redis是高性能内存Key-Value存储系统,支持丰富数据类型与持久化方案(RDB/AOF),本文给大家介绍Redis高性能Key-... 目录Redis:高性能Key-Value存储与缓存利器什么是Redis?为什么选择Redis?Red

java 恺撒加密/解密实现原理(附带源码)

《java恺撒加密/解密实现原理(附带源码)》本文介绍Java实现恺撒加密与解密,通过固定位移量对字母进行循环替换,保留大小写及非字母字符,由于其实现简单、易于理解,恺撒加密常被用作学习加密算法的入... 目录Java 恺撒加密/解密实现1. 项目背景与介绍2. 相关知识2.1 恺撒加密算法原理2.2 Ja

Nginx屏蔽服务器名称与版本信息方式(源码级修改)

《Nginx屏蔽服务器名称与版本信息方式(源码级修改)》本文详解如何通过源码修改Nginx1.25.4,移除Server响应头中的服务类型和版本信息,以增强安全性,需重新配置、编译、安装,升级时需重复... 目录一、背景与目的二、适用版本三、操作步骤修改源码文件四、后续操作提示五、注意事项六、总结一、背景与

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订