Skip to content

Commit 57d5947

Browse files
committed
Improve concurrent file access
If the device file is opened twice concurrently, closing one of them will clear configurations such as the timer and fast_buf, leading to errors in the other operation. Therefore, considering the state under concurrency, an atomic_t type variable named open_cnt is added to track the number of opens. The original settings operations will only be performed when it is the first open or the last close.
1 parent d92dffe commit 57d5947

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

simrupt.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,27 @@ static ssize_t simrupt_read(struct file *file,
282282
return ret ? ret : read;
283283
}
284284

285+
static atomic_t open_cnt;
286+
285287
static int simrupt_open(struct inode *inode, struct file *filp)
286288
{
287289
pr_debug("simrupt: %s\n", __func__);
288-
mod_timer(&timer, jiffies + msecs_to_jiffies(delay));
290+
if (atomic_inc_return(&open_cnt) == 1)
291+
mod_timer(&timer, jiffies + msecs_to_jiffies(delay));
292+
pr_info("openm current cnt: %d\n", atomic_read(&open_cnt));
293+
289294
return 0;
290295
}
291296

292297
static int simrupt_release(struct inode *inode, struct file *filp)
293298
{
294299
pr_debug("simrupt: %s\n", __func__);
295-
del_timer_sync(&timer);
296-
flush_workqueue(simrupt_workqueue);
297-
fast_buf_clear();
300+
if (atomic_dec_and_test(&open_cnt) == 0) {
301+
del_timer_sync(&timer);
302+
flush_workqueue(simrupt_workqueue);
303+
fast_buf_clear();
304+
}
305+
pr_info("release, current cnt: %d\n", atomic_read(&open_cnt));
298306

299307
return 0;
300308
}
@@ -361,6 +369,7 @@ static int __init simrupt_init(void)
361369

362370
/* Setup the timer */
363371
timer_setup(&timer, timer_handler, 0);
372+
atomic_set(&open_cnt, 0);
364373

365374
pr_info("simrupt: registered new simrupt device: %d,%d\n", major, 0);
366375
out:

0 commit comments

Comments
 (0)