try_module_get与module_put

本文详细解析了Linux内核中模块的引用计数机制,包括try_module_get和module_put函数的工作原理,以及如何在模块加载和卸载过程中正确地增加和减少引用计数,确保模块稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

try_module_get:功能:判断module模块是否处于活动状态,然后通过local_inc()宏将该模块的引用计数加1

static inline int try_module_get(struct module *module)
{
    int ret = 1;
 
    if (module) {
        unsigned int cpu = get_cpu();
        if (likely(module_is_live(module))) {
            local_inc(__module_ref_addr(module, cpu));
            trace_module_get(module, _THIS_IP_,
                local_read(__module_ref_addr(module, cpu)));
        }   
        else
            ret = 0;
        put_cpu();
    }   
    return ret;
}

module_put:功能:使指定的模块使用量减一

void module_put(struct module *module)
{
	if (module) {
		unsigned int cpu = get_cpu();
		local_dec(__module_ref_addr(module, cpu));
		/* Maybe they're waiting for us to drop reference? */
		if (unlikely(!module_is_live(module)))
			wake_up_process(module->waiter);
		put_cpu();
	}
}

使用例子:

增加引用计数,在卸载模块之前,如果该模块的计数不为0,就会卸载失败

static int
fb_open(struct inode *inode, struct file *file)
__acquires(&info->lock)
__releases(&info->lock)
{
	int fbidx = iminor(inode);
	struct fb_info *info;
	int res = 0;

	if (fbidx >= FB_MAX)
		return -ENODEV;
	info = registered_fb[fbidx];
	if (!info)
		request_module("fb%d", fbidx);
	info = registered_fb[fbidx];
	if (!info)
		return -ENODEV;
	mutex_lock(&info->lock);
//增加引用计数,在卸载模块之前,如果该模块的计数不为0,就会卸载失败
	if (!try_module_get(info->fbops->owner)) {
		res = -ENODEV;
		goto out;
	}
	file->private_data = info;
	if (info->fbops->fb_open) {
		res = info->fbops->fb_open(info,1);
		if (res)
			module_put(info->fbops->owner);
	}
#ifdef CONFIG_FB_DEFERRED_IO
	if (info->fbdefio)
		fb_deferred_io_open(info, inode, file);
#endif
out:
	mutex_unlock(&info->lock);
	return res;
}

如果卸载一个模块,使模块引用计数减少1要在module_exit函数之前调用,一般在release函数调用,这是用户空间close函数的系统调用。

static int 
fb_release(struct inode *inode, struct file *file)
__acquires(&info->lock)
__releases(&info->lock)
{
	struct fb_info * const info = file->private_data;

	mutex_lock(&info->lock);
	if (info->fbops->fb_release)
		info->fbops->fb_release(info,1);
	module_put(info->fbops->owner);
	mutex_unlock(&info->lock);
	return 0;
}

 

修改问题:未解析的引用 'StrictEventBus'未解析的引用 'LotteryCoordinator'未解析的引用 'AnalysisModule'未解析的引用 'Module4'未解析的引用 'StrictEventBus'未解析的引用 'LotteryCoordinator'未解析的引用 'AnalysisModule'第二个问题,事件C号码池既是前面模块1,模块2,模块3,模块4的数据接收者,又是模块5的数据传输者,具有双重身份,它的目的是收集前面模块传递过来的数据,打包又传递给模块5.在这里没有代码反应出来。第三个问题,不要出现数据,这样会影响数据分析的真实性。# ==================== 增强版事件总线 ==================== class EnhancedEventBus(StrictEventBus): """增强版事件总线(增加模块路由功能)""" def _process(self): while True: self._route_events('module_channel') time.sleep(0.01) def _route_events(self, channel_name: str): try: while not self.channels[channel_name].empty(): event = self.channels[channel_name].get_nowait() # 新增路由逻辑 if event.get('module') == '模块3': self._handle_module3_event(event) elif event.get('module') == '模块4': self._handle_module4_event(event) else: self.channels['notification_channel'].put(event) self.channels[channel_name].task_done() except queue.Empty: pass def _handle_module3_event(self, event): """专用模块3事件处理""" processed_event = { **event, 'metadata': {'handled_by': 'module3_handler'} } self.channels['notification_channel'].put(processed_event) def _handle_module4_event(self, event): """专用模块4事件处理""" processed_event = { **event, 'metadata': {'handled_by': 'module4_handler'} } self.channels['notification_channel'].put(processed_event) # ==================== 改进版号码池 ==================== class EnhancedNumberPool(NumberPool): """增强版号码池(支持多模块处理)""" def _register_handlers(self): bus = EnhancedEventBus() bus.channels['module_channel'].put = self._process_module_data # 注册模块3处理器 bus.channels['module_channel'].put_module3 = self._process_module3_data # 注册模块4处理器 bus.channels['module_channel'].put_module4 = self._process_module4_data def _process_module3_data(self, event: dict): """处理模块3数据""" fronts = event.get('data', {}).get('front_area', []) backs = event.get('data', {}).get('back_area', []) print(f"接收模块3数据 - 前区: {fronts} 后区: {backs}") self._store_module3_data(fronts, backs) def _process_module4_data(self, event: dict): """处理模块4数据""" analysis_data = event.get('analysis_result', {}) print("接收模块4趋势分析数据:") print(json.dumps(analysis_data, indent=2)) self._store_module4_data(analysis_data) def _store_module3_data(self, fronts, backs): """存储模块3数据""" with open(self.storage_path, 'a') as f: record = { 'timestamp': time.time(), 'type': 'module3', 'data': { 'fronts': fronts, 'backs': backs } } f.write(json.dumps(record) + '\n') def _store_module4_data(self, analysis_data): """存储模块4数据""" with open(self.storage_path, 'a') as f: record = { 'timestamp': time.time(), 'type': 'module4', 'data': analysis_data } f.write(json.dumps(record) + '\n') # ==================== 模块协调器 ==================== class EnhancedLotteryCoordinator(LotteryCoordinator): """增强版协调器(支持完整模块注册)""" def __init__(self): super().__init__() self._init_module_handlers() def _init_module_handlers(self): """初始化模块专用处理器""" # 模块3事件处理器 self.modules['module3'].handler = threading.Thread( target=self._handle_module3_events, daemon=True ) self.modules['module3'].handler.start() # 模块4事件处理器 self.modules['module4'].handler = threading.Thread( target=self._handle_module4_events, daemon=True ) self.modules['module4'].handler.start() def _handle_module3_events(self): """处理模块3事件流""" bus = EnhancedEventBus() while True: try: event = bus.channels['module_channel'].get(timeout=1) if event.get('module') == '模块3': print("处理模块3事件:", event) self.pool._process_module3_data(event) except queue.Empty: continue def _handle_module4_events(self): """处理模块4事件流""" bus = EnhancedEventBus() while True: try: event = bus.channels['module_channel'].get(timeout=1) if event.get('module') == '模块4': print("处理模块4事件:", event) self.pool._process_module4_data(event) except queue.Empty: continue # ==================== 模块实现 ==================== class Module3(AnalysisModule): """增强版模块3实现""" def submit_following_analysis(self, analysis_data: dict): """提交跟随分析结果""" super().submit({ 'type': 'following_analysis', 'data': analysis_data }) class Module4(Module4): """增强版模块4实现""" def submit_trend_analysis(self, analysis_data: dict): """提交趋势分析结果""" super().submit_analysis({ 'type': 'trend_analysis', 'data': analysis_data }) # ==================== 增强版事件总线 ==================== class EnhancedEventBus(StrictEventBus): """增强版事件总线(增加模块路由功能)""" def _process(self): while True: self._route_events('module_channel') time.sleep(0.01) def _route_events(self, channel_name: str): try: while not self.channels[channel_name].empty(): event = self.channels[channel_name].get_nowait() # 新增路由逻辑 if event.get('module') == '模块3': self._handle_module3_event(event) elif event.get('module') == '模块4': self._handle_module4_event(event) else: self.channels['notification_channel'].put(event) self.channels[channel_name].task_done() except queue.Empty: pass def _handle_module3_event(self, event): """专用模块3事件处理""" processed_event = { **event, 'metadata': {'handled_by': 'module3_handler'} } self.channels['notification_channel'].put(processed_event) def _handle_module4_event(self, event): """专用模块4事件处理""" processed_event = { **event, 'metadata': {'handled_by': 'module4_handler'} } self.channels['notification_channel'].put(processed_event) # ==================== 改进版号码池 ==================== class EnhancedNumberPool(NumberPool): """增强版号码池(支持多模块处理)""" def _register_handlers(self): bus = EnhancedEventBus() bus.channels['module_channel'].put = self._process_module_data # 注册模块3处理器 bus.channels['module_channel'].put_module3 = self._process_module3_data # 注册模块4处理器 bus.channels['module_channel'].put_module4 = self._process_module4_data def _process_module3_data(self, event: dict): """处理模块3数据""" fronts = event.get('data', {}).get('front_area', []) backs = event.get('data', {}).get('back_area', []) print(f"接收模块3数据 - 前区: {fronts} 后区: {backs}") self._store_module3_data(fronts, backs) def _process_module4_data(self, event: dict): """处理模块4数据""" analysis_data = event.get('analysis_result', {}) print("接收模块4趋势分析数据:") print(json.dumps(analysis_data, indent=2)) self._store_module4_data(analysis_data) def _store_module3_data(self, fronts, backs): """存储模块3数据""" with open(self.storage_path, 'a') as f: record = { 'timestamp': time.time(), 'type': 'module3', 'data': { 'fronts': fronts, 'backs': backs } } f.write(json.dumps(record) + '\n') def _store_module4_data(self, analysis_data): """存储模块4数据""" with open(self.storage_path, 'a') as f: record = { 'timestamp': time.time(), 'type': 'module4', 'data': analysis_data } f.write(json.dumps(record) + '\n') # ==================== 模块协调器 ==================== class EnhancedLotteryCoordinator(LotteryCoordinator): """增强版协调器(支持完整模块注册)""" def __init__(self): super().__init__() self._init_module_handlers() def _init_module_handlers(self): """初始化模块专用处理器""" # 模块3事件处理器 self.modules['module3'].handler = threading.Thread( target=self._handle_module3_events, daemon=True ) self.modules['module3'].handler.start() # 模块4事件处理器 self.modules['module4'].handler = threading.Thread( target=self._handle_module4_events, daemon=True ) self.modules['module4'].handler.start() def _handle_module3_events(self): """处理模块3事件流""" bus = EnhancedEventBus() while True: try: event = bus.channels['module_channel'].get(timeout=1) if event.get('module') == '模块3': print("处理模块3事件:", event) self.pool._process_module3_data(event) except queue.Empty: continue def _handle_module4_events(self): """处理模块4事件流""" bus = EnhancedEventBus() while True: try: event = bus.channels['module_channel'].get(timeout=1) if event.get('module') == '模块4': print("处理模块4事件:", event) self.pool._process_module4_data(event) except queue.Empty: continue # ==================== 模块实现 ==================== class Module3(AnalysisModule): """增强版模块3实现""" def submit_following_analysis(self, analysis_data: dict): """提交跟随分析结果""" super().submit({ 'type': 'following_analysis', 'data': analysis_data }) class Module4(Module4): """增强版模块4实现""" def submit_trend_analysis(self, analysis_data: dict): """提交趋势分析结果""" super().submit_analysis({ 'type': 'trend_analysis', 'data': analysis_data }) # ==================== 使用示例 ==================== if __name__ == "__main__": # 初始化协调器 coordinator = EnhancedLotteryCoordinator() # 模拟模块3数据 coordinator.submit('module3', { 'front_area': [5, 12, 23], 'back_area': [3, 8] }) # 模拟模块4数据 coordinator.submit('module4', { 'trend_analysis': { 'sum': 85, 'prime_ratio': '2:3', 'odd_even': '3:2' } }) # 等待处理完成 time.sleep(2) print("\n当前存储数据:") with open("lottery_data.json") as f: print(f.read())
05-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值