Read/Write Lock

  SomeContainer   Set<element> elements;  ReadWriteLock globalLock;  Lock readLock;  Lock writeLock; SomeContainer()   elements =  HashSet<element>();  globalLock =  ReentrantReadWriteLock();  readLock = globalLock.readLock();  writeLock = globalLock.writeLock();     addElement(Element elem)   writeLock.lock();      elements.add(elem);        writeLock.unlock();        processElements(ElementProcessor processor)   readLock.lock();      Iterator<element> iter = elements.iterator();    (iter.hasNext())      Element element = iter.next();     processor.processElement(element);        readLock.unlock();    ElementProcessor   processElement(Element element);

So, in the example above, the first line in processElements will let everybody play at once so long as the write lock is not held. Then, when the write lock is requested, the requesting thread will be made to wait until all existing readers are done (no further readers will be let in), and then the writer thread may begin.

 

http://www.javalobby.com/java/forums/t45090.html

你遇到的是一条 **编译警告**,不是错误,但为了代码健壮性和避免潜在问题,我们来分析并解决它。 --- ## ❗ 警告内容: ``` /home/332641/IMOU/SmartLock/Kernel/Linux5.10AD100/driver/log_to_file.c:42:16: warning: 'hooked_tty_write' defined but not used [-Wunused-function] ``` ### ✅ 含义解释: - 你定义了函数 `hooked_tty_write()`; - 但在整个模块中,这个函数没有被任何地方调用; - 因此编译器发出警告:函数定义了但未使用; - 由于 `-Werror`(将警告视为错误),这会导致编译失败。 --- ## ✅ 为什么会出现这个警告? 从你的 Makefile 和编译输出来看,你正在编译一个内核模块 `log_to_file.ko`。这个模块的目的是 hook `tty_write` 函数并记录输出到缓冲区。 但: - `hooked_tty_write` 是用于替换原始 `tty_write` 的钩子函数; - 如果你没有在 `log_all_console_init()` 中将它赋值给 `orig_tty_write`,那么它就不会被调用; - 或者你已经注释掉了修改函数指针的代码,导致它未被使用; - 因此编译器提示未使用函数。 --- ## ✅ 如何修复? ### ✅ 方法一:确保 `hooked_tty_write` 被使用 确保你有如下代码在 `log_all_console_init()` 中: ```c orig_tty_write = (tty_write_t)kallsyms_lookup_name("tty_write"); if (!orig_tty_write) { pr_err("Failed to find tty_write symbol\n"); return -EINVAL; } // 替换函数指针 unsigned long cr0 = read_cr0(); write_cr0(cr0 & (~0x10000)); // 清除写保护 *(unsigned long *)orig_tty_write = (unsigned long)hooked_tty_write; write_cr0(cr0); // 恢复写保护 ``` 如果你没有使用这段代码(比如你注释掉了),那 `hooked_tty_write` 就不会被使用,自然会报这个警告。 --- ### ✅ 方法二:如果确实不需要 `hooked_tty_write` 如果你只是测试代码,或者暂时不需要 hook `tty_write`,可以安全地 **删除或注释掉这个函数**。 ### ✅ 删除函数示例: ```c // 删除整个函数 // static ssize_t hooked_tty_write(...) { ... } ``` --- ### ✅ 方法三:抑制未使用警告(不推荐) 如果你保留函数但不想看到警告,可以使用 `__attribute__((unused))`: ```c static ssize_t __attribute__((unused)) hooked_tty_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { ... } ``` 这样编译器就不会报错,但你还是要确保这个函数确实暂时不需要使用。 --- ## ✅ 推荐做法总结 | 场景 | 推荐操作 | |------|-----------| | 需要 hook tty_write | 确保函数被赋值并使用 | | 暂时不使用 | 删除或注释该函数 | | 保留函数但不想报错 | 使用 `__attribute__((unused))` 抑制警告 | --- ## ✅ 示例修复后的关键代码段 ```c static int __init log_all_console_init(void) { // 获取 tty_write 地址 orig_tty_write = (tty_write_t)kallsyms_lookup_name("tty_write"); if (!orig_tty_write) { pr_err("Failed to find tty_write symbol\n"); return -EINVAL; } // 修改写保护并替换函数指针 unsigned long cr0 = read_cr0(); write_cr0(cr0 & (~0x10000)); *(unsigned long *)orig_tty_write = (unsigned long)hooked_tty_write; write_cr0(cr0); // 其他初始化代码... } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值